<?php
class Master extends CI_Model
{
    function __construct()
    {
        parent::__construct();
		$this->load->model("engine/pass_word");
    }

	// Pagination type
	var $PAGINATION_NONE					= 1;
	var $PAGINATION_NEXT_PREV				= 2;
	var $PAGINATION_SIMPLE_NUMBER			= 3;
	var $PAGINATION_ADV_NUMBER				= 4;
	var $PAGINATION_ADV_NEXT_PREV_NUMBER	= 5;
	
	// Field Value
	var $FIELD_VALUE_DEFAULT				= 0;
	var $FIELD_VALUE_AUTO					= 1;
	var $FIELD_VALUE_TIME					= 2;
	var $FIELD_VALUE_DATE					= 3;
	var $FIELD_VALUE_IP						= 4;
	var $FIELD_VALUE_USER					= 5;
	
	// Input Type		
	var $INPUT_TYPE_HIDDEN								= 0;
	var $INPUT_TYPE_TEXT								= 1;
	var $INPUT_TYPE_PASSWORD							= 2;
	var $INPUT_TYPE_RADIO								= 3;
	var $INPUT_TYPE_CHECKBOX							= 4;
	var $INPUT_TYPE_SELECT								= 5;
	var $INPUT_TYPE_IMAGE								= 6;
	var $INPUT_TYPE_FILE								= 7;
	var $INPUT_TYPE_SIMPLE_TABLE						= 8;
	var $INPUT_TYPE_MEDIUM_TABLE						= 9;
	var $INPUT_TYPE_ADVANCE_TABLE						= 10;
	var $INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE			= 11;
	var $INPUT_TYPE_MULTIPLE_VALUE_IMAGE				= 12;
	var $INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN	= 13;
	var $INPUT_TYPE_TEXT_PRIMARY						= 14;
	var $INPUT_TYPE_TEXT_AREA							= 15;
	var $INPUT_TYPE_TEXT_AREA_PLUS_EDITOR				= 16;
	
	var $INPUT_TYPE_STRING	= array(
				"hidden",					// $INPUT_TYPE_HIDDEN
				"text",						// $INPUT_TYPE_TEXT
				"password",					// $INPUT_TYPE_PASSWORD
				"radio",					// $INPUT_TYPE_RADIO
				"checkbox",					// $INPUT_TYPE_CHECKBOX
				"select",					// $INPUT_TYPE_SELECT
				"image",					// $INPUT_TYPE_IMAGE
				"file",						// $INPUT_TYPE_FILE
				"simple_table",				// $INPUT_TYPE_SIMPLE_TABLE
				"medium_table",				// $INPUT_TYPE_MEDIUM_TABLE
				"advance_table",			// $INPUT_TYPE_ADVANCE_TABLE
				"multiple_value",			// $INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE
				"multiple_value_image",		// $INPUT_TYPE_MULTIPLE_VALUE_IMAGE
				"multiple_value_foreign",	// $INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN
				"text_primary",				// $INPUT_TYPE_TEXT_PRIMARY
				"text_area",				// $INPUT_TYPE_TEXT_PRIMARY
				"text_area_plus_editor",	// $INPUT_TYPE_TEXT_PRIMARY
	);
	// attribute size of input
	var $ATTR_SIZE_SHORT			= 0;
	var $ATTR_SIZE_MEDIUM			= 1;
	var $ATTR_SIZE_LONG				= 2;

	// attribute array_field_general
	var $ARRAY_FIELD_NONE_VALUE		= false;

	// validation on field
	var $VALIDATION_NONE			= 0;
	var $VALIDATION_NOTNULL			= 1;
	var $VALIDATION_NUMERIC			= 2;
	var $VALIDATION_EMAIL			= 3;
	var $VALIDATION_DATE			= 4;
	var $VALIDATION_TIME			= 5;
	var $VALIDATION_FILE_TYPE		= 6;
	var $VALIDATION_MATCH_TABLE		= 7;
	var $VALIDATION_URL		= 8;
	
	// array index of field
	var $ARRAY_INDEX_FIELD_NAME				= 0;
	var $ARRAY_INDEX_FIELD_TEXT				= 1;
	var $ARRAY_INDEX_FIELD_VALUE			= 2;
	var $ARRAY_INDEX_FIELD_INPUT			= 3;
	var $ARRAY_INDEX_FIELD_PRIMARY			= 4;
	var $ARRAY_INDEX_FIELD_VIEW				= 5;
	var $ARRAY_INDEX_FIELD_EDIT				= 6;
	var $ARRAY_INDEX_FIELD_SOURCE			= 7;
	var $ARRAY_INDEX_FIELD_VALIDATION		= 8;
	var $ARRAY_INDEX_FIELD_FILTER			= 9;
	var $ARRAY_INDEX_FIELD_FILTER_SOURCE	= 10;
	var $ARRAY_INDEX_FIELD_NOTE				= 11;
	var $ARRAY_INDEX_FIELD_WHERE			= 12;
	var $ARRAY_INDEX_FIELD_REAL_VALUE		= 13;
	var $ARRAY_INDEX_FIELD_SOURCE_VALUE		= 14;
	
	// array index of field
	var $ARRAY_INDEX_FIELD_CROSS_TEXT			= 0;
	var $ARRAY_INDEX_FIELD_CROSS_INPUT			= 1;
	var $ARRAY_INDEX_FIELD_CROSS_SOURCE			= 2;
	var $ARRAY_INDEX_FIELD_CROSS_VALIDATION		= 3;
	var $ARRAY_INDEX_FIELD_CROSS_VALUE			= 4;
	var $ARRAY_INDEX_FIELD_CROSS_NOTE			= 5;
	var $ARRAY_INDEX_FIELD_CROSS_REAL_VALUE		= 6;
	var $ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE	= 7;
	
	// array index of setting
	var $ARRAY_INDEX_SETTING_TITLE				= 0;
	var $ARRAY_INDEX_SETTING_URL				= 1;
	var $ARRAY_INDEX_SETTING_INSERT				= 2;
	var $ARRAY_INDEX_SETTING_EDIT				= 3;
	var $ARRAY_INDEX_SETTING_DELETE				= 4;
	var $ARRAY_INDEX_SETTING_EDIT_NOT_NULL		= 5;
	var $ARRAY_INDEX_SETTING_PAGINATION_TYPE	= 6;
	var $ARRAY_INDEX_SETTING_TABLE_NAME			= 7;
	var $ARRAY_INDEX_SETTING_CROSS_RECORD		= 8;
	var $ARRAY_INDEX_SETTING_ID_USER			= 9;
	var $ARRAY_INDEX_SETTING_ACCESS				= 10;
	var $ARRAY_INDEX_SETTING_UPLOAD_PATH		= 11;
	var $ARRAY_INDEX_SETTING_BASE_URL			= 12;
	var $ARRAY_INDEX_SETTING_ADMIN_TEMPLATE		= 13;
	var $ARRAY_INDEX_SETTING_MODULE_NAME		= 14;
	var $ARRAY_INDEX_SETTING_CROSS_FIELD		= 15;
	
	// array index sub input
	var $ARRAY_INDEX_SUB_INPUT_TITLE			= 0;
	var $ARRAY_INDEX_SUB_INPUT_FIELDS			= 1;
	var $ARRAY_INDEX_SUB_INPUT_TABLE_NAME		= 2;
	var $ARRAY_INDEX_SUB_INPUT_FIELD_ID			= 3;
	var $ARRAY_INDEX_SUB_INPUT_FIELD_FOREIGN	= 4;
	
	// array index sub input field
	var $ARRAY_SUB_INPUT_INDEX_FIELD_NAME			= 0;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_TEXT			= 1;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_VALUE			= 2;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_INPUT			= 3;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE			= 4;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION		= 5;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_NOTE			= 6;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE	= 7;
	var $ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE		= 8;
	
	var $OPERATOR_NOT_EQUAL			= 0;
	var $OPERATOR_EQUAL				= 1;
	var $OPERATOR_LESS_THAN			= 2;
	VAR $OPERATOR_GREAT_THAN		= 3;
	var $OPERATOR_LESS_THAN_EQUAL	= 4;						
	var $OPERATOR_GREAT_THAN_EQUAL	= 5;
	var $OPERATOR_LIKE				= 6;
	
	var $_arraySetting			= array();
	var $_arrayField			= array();
	var $_arraySubInput			= array();
	var $_arrayDeleteRequired	= array();
	var $_arrayExtraFilter		= array();
	var $_arrayReturnValue		= array();
	
	var $_SETTING_ID_USER		= 0;
	var $_SETTING_USER_OTORITY	= "";
	var $_DEMO_MODE				= false;
	
	var $_IDUSER				= 0;
	
	function set_ID_USER($id_user)
	{
		$this->_SETTING_ID_USER = $id_user;		
	}
	
	function set_USER_OTORITY($user_otority)
	{
		$this->_SETTING_USER_OTORITY = $user_otority;		
	}
	
	function is_menu_required($url)
	{
		$sql = "SELECT `id_admin_category_akses` f_aca FROM tbl_admin_category_akses taca 
				INNER JOIN tbl_admin_user tau
					ON tau.id_category = taca.id_admin_category
				INNER JOIN tbl_menu_cms tmc
					ON tmc.id_menu = taca.id_menu
				WHERE tau.id_admin_user = '".$this->_SETTING_ID_USER."'
				AND tmc.user_otority = '".$this->_SETTING_USER_OTORITY."'
				AND tmc.url = '$url'";	
		$rec = $this->db->query($sql);
		if($rec->num_rows()>0)
			return true;
		return false;
	}
//----------------------------------------------------//
# @Page_Title 				[merge]
# @Page_URL					[merge]
# @ARRAY_SETTING
# @ARRAY_Field
# @ARRAY_View				[pending]
# @ARRAY_SUB_INPUT			
# @ARRAY_SPESIAL_VALUE		[different implement]
# @ARRAY_Delete_Required
//----------------------------------------------------//
	function generateSystem(
	$ARRAY_Setting=array()
	, $ARRAY_Field=array()
	, $ARRAY_SubInput=array()
	, $ARRAY_Delete_Required=array()
	, $ARRAY_Extra_Filter=array())
	{
		$moduleName	= $this->uri->segment(3);
		$rule		= $this->uri->segment(4);
		$command	= $this->uri->segment(5);
		$param1		= $this->uri->segment(6);
		$param2		= $this->uri->segment(7);
		$param3		= $this->uri->segment(8);
		$param4		= $this->uri->segment(9);
		$this->_arraySetting		= $ARRAY_Setting;
		$this->_arrayField			= $ARRAY_Field;
		$this->_arraySubInput		= $ARRAY_SubInput;
		$this->_arrayDeleteRequired	= $ARRAY_Delete_Required;
		$this->_arrayExtraFilter	= $ARRAY_Extra_Filter;
		$this->_arrayReturnValue["modulename"] 	= $moduleName;
		
		$this->_IDUSER			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ID_USER];
		
		if(!$this->is_menu_required($moduleName) &&($this->_SETTING_USER_OTORITY=='admin'))
			exit;
		$crossMode	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_RECORD];
		if($crossMode)
		{
			switch($command)
			{
				case "insert_cross" :
					$this->_arrayReturnValue["command"] = "insert_cross";
					$this->_arrayReturnValue["idEdit"] = "";
					$this->insertCrossModule();
					$this->_arrayReturnValue["title"] 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TITLE];
					$this->_arrayReturnValue["url"] 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_URL];
					$this->_arrayReturnValue["field"] 	= $this->convertToTextIndexCross($this->_arrayField);
					$this->_arrayReturnValue["admin_template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
				break;
				case "view_cross_data" :
				default :
					$this->_arrayReturnValue["command"] 		= "view_cross_data";
					$arrayViewData	= $this->viewCrossData();
					$this->_arrayReturnValue["title"]		 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TITLE];
					$this->_arrayReturnValue["url"] 			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_URL];
					$this->_arrayReturnValue["record"]		 	= $arrayViewData;
					$this->_arrayReturnValue["url"]				= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_BASE_URL];
					$this->_arrayReturnValue["admin_template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
				break;
		}
		}
		else
		switch($command)
		{
			case "insert" :
				$this->_arrayReturnValue["command"] = "insert";
				$this->_arrayReturnValue["idEdit"] = "";
				$this->insertModule();
				$this->_arrayReturnValue["title"] 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TITLE];
				$this->_arrayReturnValue["url"] 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_URL];
				$this->_arrayReturnValue["field"] 	= $this->convertToTextIndex($this->_arrayField);
				$this->_arrayReturnValue["admin_template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
			break;
			case "edit" :
			break;
			case "medium_table" :
				$this->_arrayReturnValue["command"] = "medium_table";
				$arrayMediumTable					= $this->mediumTableModule();
				$this->_arrayReturnValue["record"] 	= $arrayMediumTable[0];
				$this->printArr($this->_arrayReturnValue["record"], 0);
				$this->_arrayReturnValue["text"] 	= $arrayMediumTable[1];
				$this->_arrayReturnValue["module"] 	= $param1;
			break;
			case "advance_table" :
				$this->_arrayReturnValue["command"] = "medium_table";
				$arrayMediumTable					= $this->advanceTableModule();
				$this->_arrayReturnValue["record"] 	= $arrayMediumTable[0];
				$this->_arrayReturnValue["text"] 	= $arrayMediumTable[1];
				$this->_arrayReturnValue["module"] 	= $param1;
			break;
			case "multiple_value":
				$this->_arrayReturnValue["command"] = "multiple_value";
				$arrayMediumTable					= $this->MultipleValueModule();
				$this->_arrayReturnValue["record"] 	= $arrayMediumTable[0];
				$this->_arrayReturnValue["text"] 	= $arrayMediumTable[1];
				$this->_arrayReturnValue["module"] 	= $param1;
			break;
			case "multiple_value_foreign":
				$this->_arrayReturnValue["command"] = "multiple_value_foreign";
				$arrayMediumTable					= $this->MultipleValueModule();
				$this->_arrayReturnValue["record"] 	= $arrayMediumTable[0];
				$this->_arrayReturnValue["text"] 	= $arrayMediumTable[1];
				$this->_arrayReturnValue["module"] 	= $param1;
			break;
			case "detail_view" :
				$this->_arrayReturnValue["command"] 	= "detail_view";
				$fieldsSetting	= $this->getFieldsSettingViewData();
				$this->_arrayReturnValue["record"] 		= $this->getRecordsViewData(0, 1, $fieldsSetting, $param1);
				$this->_arrayReturnValue["FieldSeting"]	= $this->getFieldsSettingViewData();
				
			break;
			case "text_primary" :
				$this->_arrayReturnValue["command"] 	= "text_primary";
				$this->_arrayReturnValue["valid"] 		= $this->checkPrimary();
				$this->_arrayReturnValue["base_url"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_BASE_URL];
				$this->_arrayReturnValue["template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
			break;
			case "sub" :
				$this->_arrayReturnValue["command"] 		= "sub";
				$arrayViewData								= $this->subInput();
				$this->_arrayReturnValue["title"]		 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TITLE];
				$this->_arrayReturnValue["url"] 			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_URL];
				$this->_arrayReturnValue["record"]		 	= $arrayViewData;
				$this->_arrayReturnValue["url"]				= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_BASE_URL];
				$this->_arrayReturnValue["admin_template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
			break;
			case "single_edit":
				$this->_arrayReturnValue["command"] 		= "single_edit";
				$arrayViewData								= $this->singleEditModule();
				$this->_arrayReturnValue["title"]		 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TITLE];
				$this->_arrayReturnValue["url"] 			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_URL];
				$this->_arrayReturnValue["record"]		 	= $arrayViewData;
				$this->_arrayReturnValue["url"]				= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_BASE_URL];
				$this->_arrayReturnValue["admin_template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
				$this->printArr($this->_arrayReturnValue, false);
			break;
			case "view_data" :
			default :
				$this->_arrayReturnValue["command"] 		= "view_data";
				$arrayViewData	= $this->viewData();
				$this->_arrayReturnValue["title"]		 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TITLE];
				$this->_arrayReturnValue["url"] 			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_URL];
				$this->_arrayReturnValue["record"]		 	= $arrayViewData;
				$this->_arrayReturnValue["insert"]			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_INSERT];
				$this->_arrayReturnValue["edit"]			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_EDIT];
				$this->_arrayReturnValue["delete"]			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_DELETE];
				$this->_arrayReturnValue["url"]				= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_BASE_URL];
				$this->_arrayReturnValue["admin_template"]	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ADMIN_TEMPLATE];
				$this->_arrayReturnValue["sub_input"]		= (count($this->_arraySubInput)>0)?true:false;
			break;
		}
		return $this->_arrayReturnValue;
	}	// Module generateSystem()
	
	function deleteRecord($idDelete)
	{
		$msg		= "";
		$idMsg		= 0;
		$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$idName		= $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_NAME];
		$extraSql	= "";
		$extraSqlId	= 0;
		$this->load->model("engine/mod_sys");
		$deleteAccept = true;
		if(is_array($this->_arrayDeleteRequired) && (count($this->_arrayDeleteRequired)>0))
		foreach($this->_arrayDeleteRequired as $i=>$v)
		{
			$sql	= "SELECT `".$v[1]."` FROM `".$v[0]."` WHERE `".$v[1]."` = '$idDelete'";
			$rec	= $this->db->query($sql);
			if($rec->num_rows()>0)
			{
				$deleteAccept = false;
				$msg[$idMsg]  = "Failed to Delete, Because related with ".$v[2].".";
				$idMsg++;
			}
		}
		if($deleteAccept)
		{
			foreach($this->_arrayField as $i=>$v)
			{
				$fieldName	= $v[$this->ARRAY_INDEX_FIELD_NAME];
				switch($v[$this->ARRAY_INDEX_FIELD_INPUT][0])
				{
					case $this->INPUT_TYPE_HIDDEN:
					break;
					case $this->INPUT_TYPE_IMAGE:
						$sizeSetting	= $v[$this->ARRAY_INDEX_FIELD_VALIDATION][2];	
						$this->deleteImage($tableName, $fieldName, $idName, $idDelete, $sizeSetting);
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
					case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
						$outTableName		= $v[$this->ARRAY_INDEX_FIELD_SOURCE][3];
						$outFieldForeign	= $v[$this->ARRAY_INDEX_FIELD_SOURCE][6];
						$extraSql[$extraSqlId]	= "DELETE FROM `$outTableName` WHERE `$outFieldForeign` = '$idDelete'";
						$extraSqlId++;
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
						$sizeSetting	= $v[$this->ARRAY_INDEX_FIELD_VALIDATION][2];	
						$outTableName		= $v[$this->ARRAY_INDEX_FIELD_SOURCE][0];
						$outFieldId			= $v[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$outFieldForeign	= $v[$this->ARRAY_INDEX_FIELD_SOURCE][2];
						$outFieldData		= $v[$this->ARRAY_INDEX_FIELD_SOURCE][3];
						$sql	= "SELECT `$outFieldId` idRec FROM `$outTableName` WHERE `$outFieldForeign` = '$idDelete'";
						$rec	= $this->db->query($sql);
						$res	= $rec->result_array();
						if($rec->num_rows()>0)
							foreach($res as $key=>$val)
							{
								$this->deleteImage($outTableName, $outFieldData, $outFieldId, $val['idRec'], $sizeSetting);
							}
						$extraSql[$extraSqlId]	= "DELETE FROM `$outTableName` WHERE `$outFieldForeign` = '$idDelete'";
						$extraSqlId++;
					break;
				}
			}
			$sql		= "DELETE FROM `$tableName` WHERE `$idName` = '$idDelete'";
			if(!$this->_DEMO_MODE)
			{
				$this->db->query($sql);
				$this->printArr($extraSql, false);
				if(is_array($extraSql))
					foreach($extraSql as $esql)
					{
						$this->db->query($esql);
					}
			}
			$msg[$idMsg]  = "Delete Success.";
			$idMsg++;
		}
		$this->mod_sys->insertLog(
				$this->_SETTING_USER_OTORITY					// kategori
				, $this->_IDUSER		// kode user
				, "delete"				// type transaksi
				, $idDelete			// id table
				, $tableName);			// nama tabel
		// Input Log
		return $msg;
	}
	
	function subInput()
	{
		// type[all/data]/mode[nav]/idForeign/count[10,20,50,100,150]/start[0-999]
		// type[all/data]/mode[ins]/idForeign/fields[]
		// type[all/data]/mode[del]/idForeign/id/count[10,20,50,100,150]/start[0-999]
		// type[all/data]/mode[edt]/idForeign/id/fields_name/value
		// 
		$returnData['msg']	= "";
		$msg				= "";
		$returnData			= array();
		$filterField		= array();
		$fieldsSetting		= array();
		$records			= array();
		$pagination			= array();
		$type 					= $this->uri->segment(6);
		$mode 					= $this->uri->segment(7);
		$returnData['type']		= $type;
		$returnData['mode']		= $mode;
		$idForeign				= $this->uri->segment(8);
		$returnData['idMaster']	= $idForeign;
		switch($mode)
		{
			case "ins" 	:
//				$firstField			= $this->uri->segment(9);
				$firstField			= $this->input->post(0);
				$startUriField		= 9;
				if(($firstField!=''))
				{
					$insertValid	= true;
					foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $i=>$af)
					{	
						$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
								[$i]
								[$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	
									= "";
						$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
								[$i]
								[$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] 
									= "";
						$moduleName	= $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME];
						$moduleText	= $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT];
						switch($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0])
						{
							case $this->INPUT_TYPE_TEXT:
							case $this->INPUT_TYPE_TEXT_AREA:
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] = "";
								//$post_value	= $this->uri->segment($startUriField+$i);
								$post_value	= $this->input->post($i);
								$post_value	= ($post_value=='-')?"":$post_value;
								$post_value	= str_replace("%20", " ", $post_value);
		
								$returnValueContainer	= $this->checkValidation(
								$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
								, $post_value
								, $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
								);
								if(!$returnValueContainer['valid'])
								{
									$insertValid = false;
									$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
								}
								$realValue	= $this->fillInput($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALUE]);
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
							break;
							case $this->INPUT_TYPE_RADIO:
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] = 
									$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE];
								//$post_value	= $this->uri->segment($startUriField+$i);
								$post_value	= $this->input->post($i);
								$post_value	= ($post_value=='-')?"":$post_value;
								$post_value	= str_replace("%20", " ", $post_value);
								
								$returnValueContainer	= $this->checkValidation(
								$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
								, $post_value
								, $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
								);
								if(!$returnValueContainer['valid'])
								{
									$insertValid = false;
									$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
								}
								$realValue	= $this->fillInput($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALUE]);
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
							break;
							case $this->INPUT_TYPE_SELECT:
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] = 
									$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE];
								//$post_value	= $this->uri->segment($startUriField+$i);
								$post_value	= $this->input->post($i);
								$post_value	= ($post_value=='-')?"":$post_value;
								$post_value	= str_replace("%20", " ", $post_value);
								
								$returnValueContainer	= $this->checkValidation(
								$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
								, $post_value
								, $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
								);
								if(!$returnValueContainer['valid'])
								{
									$insertValid = false;
									$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
								}
								$realValue	= $this->fillInput($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALUE]);
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	= $post_value;
							break;
							case $this->INPUT_TYPE_SIMPLE_TABLE:
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] = 
									$this->getSimpleTableList($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]);
								$post_value	= $this->uri->segment($startUriField+$i);
								$post_value	= $this->input->post($i);
								//$post_value	= ($post_value=='-')?"":$post_value;
								$post_value	= str_replace("%20", " ", $post_value);
								
								$returnValueContainer	= $this->checkValidation(
								$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
								, $post_value
								, $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
								);
								if(!$returnValueContainer['valid'])
								{
									$insertValid = false;
									$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
								}
								$realValue	= $this->fillInput($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALUE]);
								$realValue = ($post_value!=$realValue)?$post_value:$realValue;
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	= $post_value;
							break;
							case $this->INPUT_TYPE_MEDIUM_TABLE:
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] = 
									$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE];
								$post_value	= $this->uri->segment($startUriField+$i);
								$post_value	= $this->input->post($i);
								//$post_value	= ($post_value=='-')?"":$post_value;
								$post_value	= str_replace(array("%20", "-"), " ", $post_value);
								$detail_value		= $this->getMediumTableList(
																			$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]	
																			, "", 1, $post_value);
								$returnValueContainer	= $this->checkValidation(
								$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
								, $post_value
								, $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
								);
								$post_value	= array($post_value, $detail_value);
								if(!$returnValueContainer['valid'])
								{
									$insertValid = false;
									$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
								}
								$realValue	= $this->fillInput($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALUE]);
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
							break;
							case $this->INPUT_TYPE_ADVANCE_TABLE:
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS][$i][$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] = 
									$this->getAdvanceTableFilterSource($af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE][4]);
								$post_value	= $this->uri->segment($startUriField+$i);
								$post_value	= $this->input->post($i);
								//$post_value	= ($post_value=='-')?"":$post_value;
								$post_value	= str_replace(array("%20", "-"), " ", $post_value);
								$detail_value		= $this->getAdvanceTableList(
									$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]
									, 1, $post_value, false);
								$returnValueContainer	= $this->checkValidation(
								$af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
								, $post_value
								, $af[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
								);
								$post_value	= array($post_value, $detail_value);
								if(!$returnValueContainer['valid'])
								{
									$insertValid = false;
									$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
								}
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
									[$i]
									[$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE] = $post_value;
							break;
						} // switch value type
					} // foreach value
					$returnData['msg'] = $msg;
					if($insertValid)
					{
						$this->insertRecordSubData($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS], $idForeign);	
						foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $i=>$af)
						{	
							$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
									[$i]
									[$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	
										= "";
						}
					}
				}
				else
				foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $iif=>$sif)
				{
					$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
							[$iif]
							[$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE]	
								= "";
					$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
							[$iif]
							[$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE] 
								= "";
					switch($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0])
					{
						case $this->INPUT_TYPE_RADIO:
						case $this->INPUT_TYPE_SELECT:
						case $this->INPUT_TYPE_MEDIUM_TABLE:
							$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
								[$iif]
								[$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE]
								= 
								$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
								[$iif]
								[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]
								;
						break;
						case $this->INPUT_TYPE_SIMPLE_TABLE:
							$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
								[$iif]
								[$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE]
								= 
								$this->getSimpleTableList(
									$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
									[$iif]
									[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]
								)
								;
						break;
						case $this->INPUT_TYPE_ADVANCE_TABLE:
							$this->printArr($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE][4], false);
							$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
								[$iif]
								[$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE]
								= 
								$this->getAdvanceTableFilterSource(
									$this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS]
									[$iif]
									[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE][4]
								)
								;
						break;
					}
				}
				$returnData['fields']			= $this->convertToTextIndex($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS], true);
//				$this->getFieldsSettingViewSubData();
			break;
			case "edt" 	:
				// type[all/data]/mode[edt]/idForeign/id/fields_name/value
				$idEdt		= $this->uri->segment(9);
				$fldEdt		= $this->uri->segment(10);
				$valEdt		= $this->uri->segment(11);
				$valEdt		= rawurldecode($valEdt);
				$fldValue	= $this->uri->segment(12);
				$table_name 	= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_TABLE_NAME];
				$fieldId		= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELD_ID];
				$fieldIdValue	= $idEdt;
				$field			= "";
				$returnData['input']		= "";
				$returnData['interface']	= "";
				$returnData['value']		= $valEdt;
				$returnData['fieldname']	= $fldEdt;
				$returnData['fieldidvalue']	= $fieldIdValue;
				$returnData['msg']			= "";
				$insertValid = true;
				foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $sif)
				{
//					echo $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME]."=".$fldEdt."<br>";
					if($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME] == $fldEdt)
					{
						$returnData['input']	= $this->INPUT_TYPE_STRING[$sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0]];
						$returnData['interface']	= "edit";
						if ($valEdt!="")
						{
							$returnValueContainer	= $this->checkValidation(
							$sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_VALIDATION]
							, $valEdt
							, $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT]
							);
							$this->printArr($returnValueContainer, false);
							if((!$returnValueContainer['valid']) || ($returnValueContainer['valid']==''))
							{
								$insertValid = false;
								$returnData['msg'] = array($returnValueContainer['msg'],$fldEdt);
							}
							if($insertValid)
							{
								$this->updateSpecificFieldFromTable($table_name, $fldEdt, $valEdt, $fieldId, $fieldIdValue);
								$returnData['interface']	= "view";
							}
						}
						$returnData['value']		= $this->getFieldValueFromRecord($table_name, $fldEdt, $fieldId, $fieldIdValue);
						$inputType					= $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0];
						$fieldSource				= $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE];
						$returnData['value'] 		= $this->convertGeneralValtoInputVal($returnData['value'], $inputType, $fieldSource);
						$returnData['source']		= "";
						$returnData['attr']			= "";
						switch($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0])
						{
							case $this->INPUT_TYPE_TEXT:
								$maxlength	= $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][1]['MAX_LENGTH'];
								$maxlength	= ($maxlength>0)?$maxlength:50;
								$size		= $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][1]['SIZE'];
								switch($size)
								{
									case 0:
										$size = 20;
									break;
									case 1:
										$size = 40;
									break;
									case 2:
										$size = 80;
									break;
								}
			
								$returnData['attr']	= array(
															"maxlength"	=> $maxlength,
															"size"		=> $size,
															);
							case $this->INPUT_TYPE_TEXT_AREA:
								if(isset($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][1]['ROWS']))
								{
									$rows		= $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][1]['ROWS'];
									$cols		= $sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][1]['COLS'];
									switch($cols)
									{
										case 0:
											$cols = 20;
										break;
										case 1:
											$cols = 40;
										break;
										case 2:
											$cols = 80;
										break;
									}
				
									$returnData['attr']	= array(
																"cols"		=> $cols,
																"rows"		=> $rows,
																);
								}
							break;
							case $this->INPUT_TYPE_RADIO:
							case $this->INPUT_TYPE_SELECT:
							case $this->INPUT_TYPE_MEDIUM_TABLE:
								$returnData['source'] = 
								$sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]; 
							break;
							case $this->INPUT_TYPE_SIMPLE_TABLE:
								$returnData['source'] =
								$this->getSimpleTableList($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]);
							break;
							case $this->INPUT_TYPE_ADVANCE_TABLE:
								$returnData['source'] = 
								$this->getAdvanceTableFilterSource($sif[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE][4]);
							break;
						}
					}
				}
				$returnData['msg']				= $this->subInputEditData($table_name, $fieldId, $fieldIdValue, $field, $valEdt);
			break;
			case "del" 	:
				$idDel	= $this->uri->segment(9);
				$tableName	= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_TABLE_NAME];
				$fieldId	= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELD_ID];
				$this->subInputDelData($idDel, $tableName, $fieldId);
				$count	= $this->uri->segment(10);
					$count = ((!is_numeric($count)) || ($count==0))?10:$count;
				$start 	= $this->uri->segment(11);
					$start = ((!is_numeric($start)) || ($start==0))?1:$start;
				$start--;
				$start 	*= $count; 	
				$record							= $this->subInputGetData($idForeign, $start, $count);
				$fieldsSetting					= $this->getFieldsSettingViewData();
				$returnData['master']			= $this->getRecordsViewData(0, 1, $fieldsSetting, $idForeign, false);
				$returnData['fieldSetting']		= $fieldsSetting;
				$this->printArr($returnData['master'], false);
				$returnData['records']			= $record[0];
				$returnData['recTotal']			= $record[1];
				$returnData['fields']			= $this->getFieldsSettingViewSubData();
				$returnData['recStart']			= $start;
				$returnData['recPage']			= $count;
			break;
			case "nav" 	:
			default 	: 
				$count	= $this->uri->segment(9);
					$count = ((!is_numeric($count)) || ($count==0))?10:$count;
				$start 	= $this->uri->segment(10);
					$start = ((!is_numeric($start)) || ($start==0))?1:$start;
				$start--;
				$start 	*= $count; 	
				$record							= $this->subInputGetData($idForeign, $start, $count);
				$fieldsSetting					= $this->getFieldsSettingViewData();
				$returnData['master']			= $this->getRecordsViewData(0, 1, $fieldsSetting, $idForeign, false);
				$returnData['fieldSetting']		= $fieldsSetting;
				$this->printArr($returnData['master'], false);
				$returnData['records']			= $record[0];
				$returnData['recTotal']			= $record[1];
				$returnData['fields']			= $this->getFieldsSettingViewSubData();
				$returnData['recStart']			= $start;
				$returnData['recPage']			= $count;
			break;
		}
		return $returnData;
	}
	
	function subInputGetData($idForeign, $start, $count, $field = "")
	{
		$this->printArr($this->_arraySubInput, false);
		$sqlFields		= "";
		$tableName 		= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_TABLE_NAME];
		$fieldId 		= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELD_ID];
		$fieldForeign 	= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELD_FOREIGN];
		if($field!="")
			$sqlFields .= "`".$field."`, ";
		else
		foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $i=>$f)
		{
			$fieldName	= $f[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME];
			switch($f[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_TEXT:
				case $this->INPUT_TYPE_RADIO:
				case $this->INPUT_TYPE_SELECT:
				case $this->INPUT_TYPE_TEXT_AREA:
				case $this->INPUT_TYPE_SIMPLE_TABLE:
				case $this->INPUT_TYPE_MEDIUM_TABLE:
				case $this->INPUT_TYPE_ADVANCE_TABLE:
					$sqlFields .= "`".$fieldName."`, ";
			}
		}

		$sqlFields 	.= "`".$fieldId."` idRec, ";
		$sqlFields 	= substr($sqlFields, 0 , strlen($sqlFields)-2);
		$sql	= "SELECT $sqlFields FROM `$tableName` "
				."WHERE `$fieldForeign` = '$idForeign' ";
		
		$rec		= $this->db->query($sql);
		$recTotal	= $rec->num_rows();

		$sql	.= "LIMIT $start, $count";
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
		for($i = 0; $i < count($res); $i++)
		{
			foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $j=>$fS)
			{
				$value		= $fS[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE];
				$fieldName	= $fS[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME];
				if(($field=="") || ($field!="") && ($field==$fieldName))
				switch($fS[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0])
				{
					case $this->INPUT_TYPE_TEXT:
					case $this->INPUT_TYPE_TEXT_AREA:
					break;
					case $this->INPUT_TYPE_RADIO:
					case $this->INPUT_TYPE_SELECT:
						$fieldValue	= $res[$i][$fieldName];
						if(is_array($value))
						foreach($value as $v)
						{
							if($v[0]==$fieldValue)
							{
								$res[$i][$fieldName] = $v;
							}
						}
					break;
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$fieldValue	= $res[$i][$fieldName];
						$res[$i][$fieldName]	= $this->getSimpleTableList($value, $fieldValue);
						if(is_array($res[$i][$fieldName]) && (count($res[$i][$fieldName])>0))
							$res[$i][$fieldName]	= $res[$i][$fieldName][0];
					break;
					case $this->INPUT_TYPE_MEDIUM_TABLE:
						$fieldValue	= $res[$i][$fieldName];
						$res[$i][$fieldName]	= $this->getMediumTableList($value, "", 1, $fieldValue);
						if(is_array($res[$i][$fieldName]))
							$res[$i][$fieldName]	= $res[$i][$fieldName];
					break;
					case $this->INPUT_TYPE_ADVANCE_TABLE:
						$fieldValue	= $res[$i][$fieldName];
						$res[$i][$fieldName]	= $this->getAdvanceTableList($value, 1, $fieldValue, false);
						$this->printArr($res[$i][$fieldName], false);
						if(is_array($res[$i][$fieldName]))
							$res[$i][$fieldName]	= $res[$i][$fieldName];
					break;
				}
			}
		}
		$this->printArr(array($res, $recTotal), false);
		return array($res, $recTotal);
		
	}
	
	function convertGeneralValtoInputVal($val, $inputType, $fieldSource)
	{
		$ret = $val;
		switch($inputType)
		{
			case $this->INPUT_TYPE_TEXT:
			case $this->INPUT_TYPE_TEXT_AREA:
			break;
			case $this->INPUT_TYPE_RADIO:
			case $this->INPUT_TYPE_SELECT:
				$fieldValue	= $ret;
				if(is_array($fieldSource))
				foreach($fieldSource as $v)
				{
					if($v[0]==$fieldValue)
					{
							$ret = $v;
					}
				}
			break;
			case $this->INPUT_TYPE_SIMPLE_TABLE:
				$fieldValue	= $ret;
				$ret	= $this->getSimpleTableList($fieldSource, $fieldValue);
				if(is_array($ret) && (count($ret)>0))
					$ret	= $ret[0];
			break;
			case $this->INPUT_TYPE_MEDIUM_TABLE:
				$fieldValue	= $ret;
				$ret	= $this->getMediumTableList($fieldSource, "", 1, $fieldValue);
			break;
			case $this->INPUT_TYPE_ADVANCE_TABLE:
				$fieldValue	= $ret;
				$ret	= $this->getAdvanceTableList($fieldSource, 1, $fieldValue, false);
				$this->printArr($ret, false);
			break;
		}
		return $ret;
	}
	
	function subInputDelData($idDel, $tableName, $fieldId)
	{
		$sql = "DELETE FROM `$tableName` WHERE `$fieldId` = '$idDel'";
//		echo $sql;
		$this->db->query($sql);
	}
	
	function subInputEditData($table_name, $fiedId, $fieldIdValue, $field, $fieldValue)
	{
		
	}
	
	function insertRecordSubData($newFields, $idForeign)
	{
		$tableName			= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_TABLE_NAME];
		$fieldForeign		= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELD_FOREIGN];
		$tableName			= $this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_TABLE_NAME];
		$sql				= "INSERT INTO `".$tableName."` ";
		$sqlFields			= "(`$fieldForeign`, ";
		$sqlValues			= "('$idForeign', ";
		foreach($newFields as $i=>$f)
		{
			$fieldName	= $f[$this->ARRAY_INDEX_FIELD_NAME];
			$fieldValue	= $f[$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE];
			switch($f[$this->ARRAY_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_TEXT:
				case $this->INPUT_TYPE_TEXT_AREA:
				case $this->INPUT_TYPE_RADIO:
				case $this->INPUT_TYPE_SELECT:
				case $this->INPUT_TYPE_SIMPLE_TABLE:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue."', ";
				break;
				case $this->INPUT_TYPE_MEDIUM_TABLE:
				case $this->INPUT_TYPE_ADVANCE_TABLE:
					$this->printArr($fieldValue, false);
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue[0]."', ";
				break;
			}
		}
		$sqlFields = substr($sqlFields, 0, strlen($sqlFields)-2);
		$sqlValues = substr($sqlValues, 0, strlen($sqlValues)-2);
		$sqlFields .= ") ";
		$sqlValues .= ") ";
		$sql		= $sql.$sqlFields." VALUES ".$sqlValues;
		$this->db->query($sql);
	}

	function getFieldValueFromRecord($tableName, $fieldName, $fieldId, $fieldIdValue)
	{
		$sql 	= "SELECT `$fieldName` dataRec FROM `$tableName` WHERE `$fieldId` = '$fieldIdValue'";
		$rec 	= $this->db->query($sql);
		if($rec->num_rows()>0)
		{
			$data = $rec->result_array();
			return $data[0]['dataRec'];
		}
		return "";
	}
	
	function updateSpecificFieldFromTable($table_name, $fldEdt, $fldEdtValue, $fieldId, $fieldIdValue)
	{
		$sql 	= "UPDATE `$table_name` SET `$fldEdt` = '$fldEdtValue' WHERE `$fieldId` = '$fieldIdValue'";
		$this->db->query($sql);
	}
	function viewData()
	{
		// type[all/data]/start[0-999]/count[10,20,50,100,150]/filter()	
		// ex:
		// view record
		// generate filter field
		// 
		$returnData['msg']	= "";
		$returnData		= array();
		$filterField	= array();
		$fieldsSetting	= array();
		$records		= array();
		$pagination		= array();
		$type 	= $this->uri->segment(6);
		$start 	= $this->uri->segment(8);
			$start = ((!is_numeric($start)) || ($start==0))?1:$start;
		$start--;
		$count	= $this->uri->segment(7);
			$count = ((!is_numeric($count)) || ($count==0))?10:$count;
		$start 	*= $count; 
		$deleteParam 	= $this->uri->segment(9);
		if($deleteParam=='delete')
		{
			$idDelete 	= $this->uri->segment(10);
			if($this->_arraySetting[$this->ARRAY_INDEX_SETTING_DELETE])
				$returnData['msg']	= $this->deleteRecord($idDelete);
			else
				$returnData['msg'] = "Cannot Delete.";
		}
		$fieldsSetting	= $this->getFieldsSettingViewData();
		switch ($type)
		{
			case "print":
				$returnData['mode']	= 'print';
				$filterField		= $this->getFilterFieldViewData();
				$records			= $this->getRecordsViewData($start, $count, $fieldsSetting);
			break;
			case "data":
				$returnData['mode']	= 'data';
				$filterField		= $this->getFilterFieldViewData();
				$records			= $this->getRecordsViewData($start, $count, $fieldsSetting);
			break;
			case "all":
			default:
				$returnData['mode']	= 'all';
				$filterField		= $this->getFilterFieldViewData();
				$records			= $this->getRecordsViewData($start, $count, $fieldsSetting);
			break;
		}
		$this->printArr($filterField, false);
		$returnData['filterValue']		= $this->getFilterFieldValue(9);
		$returnData['filterField']		= $filterField;
		$returnData['fieldSetting']		= $fieldsSetting;
		$returnData['records']			= $records[0];
		$returnData['recTotal']			= $records[1];
		$returnData['recStart']			= $start;
		$returnData['recPage']			= $count;
		$returnData['navigate']			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_PAGINATION_TYPE];
		$this->printArr($returnData, false);
		return $returnData;
	}
	
	function viewCrossData()
	{
		// ex:
		// view record
		// generate filter field
		// 
		$returnData['msg']	= "";
		$returnData		= array();
		$fieldsSetting	= array();
		$records		= array();
		$records		= $this->getRecordsViewCrossData();
		$fieldsSetting	= $this->getFieldsSettingViewCrossData();
		$returnData['fieldSetting']		= $fieldsSetting;
		$returnData['records']			= $records;
		$this->printArr($returnData, false);
		return $returnData;
	}
	
	function getRecordsViewData($start, $count, $fieldSetting, $id = '', $where = true)
	{
		$sqlWhere	= "(1=1 AND ";
		$sqlFields	= "";
		$fieldWhere	= "";
		$iFilter	= 9;
		if($this->uri->segment($iFilter)=='delete')
			$iFilter	= 11;
		$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$fieldId	= "";
		$innerSql	= "";
		$innerWhere = "";
		if(count($this->_arrayExtraFilter)>0)
		{
			foreach($this->_arrayExtraFilter as $i=>$inner)
			{
				$aliasInnerTable = (isset($inner[0][1]))?$inner[0][1]:$inner[0][0];
				$innerSql .= " INNER JOIN ".$inner[0][0]." ".$aliasInnerTable
								." ON ".$inner[1][1].".".$inner[3]." = ".$aliasInnerTable.".".$inner[2];

				$whereforInner	 = $inner[4];
				$innerWhere .= " AND $aliasInnerTable.".$whereforInner[0];
				$valueWhere = $whereforInner[2];
				switch($whereforInner[1])
				{
					case $this->OPERATOR_EQUAL:
						$innerWhere	.= "= '$valueWhere'";
					break;
					case $this->OPERATOR_NOT_EQUAL:
						$innerWhere	.= "<> '$valueWhere'";
					break;
					case $this->OPERATOR_LESS_THAN:
						$innerWhere	.= "< $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN:
						$innerWhere	.= "> $valueWhere";
					break;
					case $this->OPERATOR_LESS_THAN_EQUAL:
						$innerWhere	.= "<= $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN_EQUAL:
						$innerWhere	.= ">= $valueWhere";
					break;
					case $this->OPERATOR_LIKE:
						$innerWhere	.= "LIKE '%$valueWhere%'";
					break;
				}
			}
		}
		foreach($this->_arrayField as $i=>$f)
		{
			$fieldName	= $f[$this->ARRAY_INDEX_FIELD_NAME];
			if(isset($f[$this->ARRAY_INDEX_FIELD_WHERE]))
			{
				$fieldWhere .= " AND $tableName.$fieldName";
				$valueWhere	= $f[$this->ARRAY_INDEX_FIELD_WHERE][1];
				switch($f[$this->ARRAY_INDEX_FIELD_WHERE][0])
				{
					case $this->OPERATOR_EQUAL:
						$fieldWhere	.= "= '$valueWhere'";
					break;
					case $this->OPERATOR_NOT_EQUAL:
						$fieldWhere	.= "<> '$valueWhere'";
					break;
					case $this->OPERATOR_LESS_THAN:
						$fieldWhere	.= "< $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN:
						$fieldWhere	.= "> $valueWhere";
					break;
					case $this->OPERATOR_LESS_THAN_EQUAL:
						$fieldWhere	.= "<= $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN_EQUAL:
						$fieldWhere	.= ">= $valueWhere";
					break;
					case $this->OPERATOR_LIKE:
						$fieldWhere	.= "LIKE '%$valueWhere%'";
					break;
				}
			}
			if($i==0)
				$fieldId = $fieldName;
			switch($f[$this->ARRAY_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_HIDDEN:
				case $this->INPUT_TYPE_TEXT_PRIMARY:
				case $this->INPUT_TYPE_TEXT:
				case $this->INPUT_TYPE_RADIO:
				case $this->INPUT_TYPE_CHECKBOX:
				case $this->INPUT_TYPE_IMAGE:
				case $this->INPUT_TYPE_FILE:
				case $this->INPUT_TYPE_SELECT:
				case $this->INPUT_TYPE_TEXT_AREA:
				case $this->INPUT_TYPE_TEXT_AREA_PLUS_EDITOR:
				case $this->INPUT_TYPE_SIMPLE_TABLE:
				case $this->INPUT_TYPE_MEDIUM_TABLE:
				case $this->INPUT_TYPE_ADVANCE_TABLE:
					$sqlFields .= "$tableName.$fieldName, ";
					if($f[$this->ARRAY_INDEX_FIELD_FILTER])
					{
						$value 		= $this->uri->segment($iFilter);
						$value 		= (($value=="-"))?"":$value;
						$sqlWhere	.= "$tableName.$fieldName like '%".$value."%' AND ";
						$iFilter++;
					}
				break;
				case $this->INPUT_TYPE_PASSWORD:
					$sqlFields .= "$tableName.$fieldName, ";
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
				break;
			}
		}
		$sqlFields 	= substr($sqlFields, 0 , strlen($sqlFields)-2);
			$sqlWhere = substr($sqlWhere, 0 , strlen($sqlWhere)-4).")";
		$sql	= "SELECT $sqlFields FROM `$tableName` ".$innerSql;
		if($where)
			$sql	.= "WHERE $sqlWhere"
				.(($id=='')?"":" AND $tableName.$fieldId='".$id."'");
		else
			$sql	.= (($id=='')?"":"WHERE $tableName.$fieldId='".$id."'");
		if(strpos($sql, "WHERE")>-1)
			$sql 	.= $fieldWhere;
		else
			$sql	.= "WHERE 1=1".$fieldWhere;
			$sql	.= $innerWhere;
//		echo $sql;		
		$rec		= $this->db->query($sql);
		$recTotal	= $rec->num_rows();

		$sql	.= " ORDER BY 1 DESC";
		$sql	.= " LIMIT $start, $count";
		//		echo $sql;
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
		for($i = 0; $i < count($res); $i++)
		{
			foreach($fieldSetting as $j=>$fS)
			{
				$value		= $this->_arrayField[$j][$this->ARRAY_INDEX_FIELD_SOURCE];
				$fieldName	= $this->_arrayField[$j][$this->ARRAY_INDEX_FIELD_NAME];
				switch($fS['input'])
				{
					case "hidden":
					case "text":
					break;
					case "password":
						$fieldValue	= $res[$i][$fieldName];
						$res[$i][$fieldName] 	= $this->pass_word->decryptPass($fieldValue);
					break;
					case "checkbox":
						$fieldValue	= $res[$i][$fieldName];
						$splitValue				= explode("|",$fieldValue);
						$res[$i][$fieldName] 	= $splitValue;
					break;
					case "image":
						if($res[$i][$fieldName]!='')
							if(isset($fS['specific'][0]) & ($fS['specific'][0]!=''))
								$res[$i][$fieldName] = 
									$this->getNameofFileName($res[$i][$fieldName])
									."-".$fS['specific'][0]
									.".".$this->getTypeofFileName($res[$i][$fieldName]);
					break;
					case "radio":
					case "select":
						if($res[$i][$fieldName]!="")
						{
							$fieldValue	= $res[$i][$fieldName];
							if(is_array($value))
							foreach($value as $v)
							{
								if($v[0]==$fieldValue)
								{
									$res[$i][$fieldName] = $v;
								}
							}
						}
					break;
					case "simple_table":
						if($res[$i][$fieldName]!="")
						{
							$fieldValue	= $res[$i][$fieldName];
							$res[$i][$fieldName]	= $this->getSimpleTableList($value, $fieldValue);
							if(is_array($res[$i][$fieldName]) && (count($res[$i][$fieldName])>0))
								$res[$i][$fieldName]	= $res[$i][$fieldName][0];
						}
					break;
					case "medium_table":
						if($res[$i][$fieldName]!="")
						{
							$fieldValue	= $res[$i][$fieldName];
							$res[$i][$fieldName]	= $this->getMediumTableList($value, "", 1, $fieldValue);
							if(is_array($res[$i][$fieldName]))
								$res[$i][$fieldName]	= $res[$i][$fieldName];
						}
					break;
					case "advance_table":
						if($res[$i][$fieldName]!="")
						{
							$fieldValue	= $res[$i][$fieldName];
							$res[$i][$fieldName]	= $this->getAdvanceTableList($value, 1, $fieldValue, false);
							if(is_array($res[$i][$fieldName]))
								$res[$i][$fieldName]	= $res[$i][$fieldName];
						}
					break;
					case "multiple_value":
						$idData	= $res[$i][$fieldSetting[0]['name']];
						$res[$i][$fieldName]	= $this->getMultipleValueData($value, $idData);
						if(is_array($res[$i][$fieldName]))
							$res[$i][$fieldName]	= $res[$i][$fieldName];
						
					break;
					case "multiple_value_foreign":
						$idData	= $res[$i][$fieldSetting[0]['name']];
						$res[$i][$fieldName]	= $this->getMultipleValueData($value, $idData, true);
						if(is_array($res[$i][$fieldName]))
							$res[$i][$fieldName]	= $res[$i][$fieldName];
						
					break;
					case "multiple_value_image":
						$idData	= $res[$i][$fieldSetting[0]['name']];
						$res[$i][$fieldName]	= $this->getMultipleValueImage($value, $idData);
						if(is_array($res[$i][$fieldName]))
							if(isset($fS['specific'][0]) & ($fS['specific'][0]!=''))
								foreach($res[$i][$fieldName] as $key=>$val)
								{
									$res[$i][$fieldName][$key]['dataRec'] = 
									$this->getNameofFileName($val['dataRec'])
									."-".$fS['specific'][0]
									.".".$this->getTypeofFileName($val['dataRec']);
								
									$res[$i][$fieldName][$key]['idRec'] = $val['idRec'];
								}
					break;
				}
			}
		}
		return array($res, $recTotal);
	}
	
	function getFilterFieldValue($start)
	{
		$ret 	= array();
		$i		= 0;
		foreach($this->_arrayField as $f)
		{
			if($f[$this->ARRAY_INDEX_FIELD_FILTER])
			{
				$value 		= $this->uri->segment($start);
				$ret[$i]	= $value;
				$i++;
				$start++;
			}
		}
		$this->printArr($ret, false);
		return $ret;
	}
	
	function getRecordsViewCrossData($idVal = '')
	{
		$sql		= "";
		$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$fieldId	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][0];
		$fieldData	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][1];
		$fieldExtra	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][2];
		$sql		= "SELECT `$fieldId` fid, `$fieldData` fdt ";
		foreach($fieldExtra as $i=>$v)
		{
			$sql	.= ", `$v` f$i";
		}
		$sql		.= " FROM `$tableName`";
		$where		= " WHERE";
		if ($idVal=="")
			foreach($this->_arrayField as $i=>$f)
			{
				if($i>0)
					$where	.= " OR";
				$fieldIdValue	= $f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE];
				switch($f[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
				{
					case $this->INPUT_TYPE_TEXT:
					case $this->INPUT_TYPE_RADIO:
					case $this->INPUT_TYPE_IMAGE:
					case $this->INPUT_TYPE_SELECT:
					case $this->INPUT_TYPE_TEXT_AREA:
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$where	.= " $fieldId = '$fieldIdValue'";
				}
			}
		else
			$where	.= " `$fieldId` = '$idVal'";
			$sql	.= $where;
//		echo $sql;		
		$rec		= $this->db->query($sql);
		$res	= $rec->result_array();
		$fieldData	= 'fdt';
		for($i = 0; $i < count($res); $i++)
		{
			foreach($this->_arrayField as $f)
			{
				$value		= $f[$this->ARRAY_INDEX_FIELD_CROSS_SOURCE];
				$validation	= $f[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION];
				if($res[$i]['fid']==$f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE])
				{
					switch($f[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
					{
						case $this->INPUT_TYPE_TEXT:
						case $this->INPUT_TYPE_TEXT_AREA:
						break;
						case $this->INPUT_TYPE_IMAGE:
							if($res[$i][$fieldData]!='')
								$specific	= "";
								if(count($validation[2])>0)
									foreach($validation[2] as $ci)
									{
										if(isset($ci['thumb_admin']) && ($ci['thumb_admin']==true))
											$specific	= $ci['initial'];
									}
									
								if(($specific!="") && ($res[$i][$fieldData]!=""))
									$res[$i][$fieldData] = 
										$this->getNameofFileName($res[$i][$fieldData])
										."-".$specific
										.".".$this->getTypeofFileName($res[$i][$fieldData]);
						break;
						case $this->INPUT_TYPE_RADIO:
						case $this->INPUT_TYPE_SELECT:
							if($res[$i][$fieldData]!="")
							{
								$fieldValue	= $res[$i][$fieldData];
								if(is_array($value))
								foreach($value as $v)
								{
									if($v[0]==$fieldValue)
									{
										$res[$i][$fieldData] = $v;
									}
								}
							}
						break;
						case $this->INPUT_TYPE_SIMPLE_TABLE:
							$value	= $f[$this->ARRAY_INDEX_FIELD_CROSS_SOURCE];
							if($res[$i][$fieldData]!="")
							{
								$fieldValue	= $res[$i][$fieldData];
								$res[$i][$fieldData]	= $this->getSimpleTableList($value, $fieldValue);
								if(is_array($res[$i][$fieldData]) && (count($res[$i][$fieldData])>0))
									$res[$i][$fieldData]	= $res[$i][$fieldData][0];
							}
						break;
	/*
						case "medium_table":
							if($res[$i][$fieldName]!="")
							{
								$fieldValue	= $res[$i][$fieldName];
								$res[$i][$fieldName]	= $this->getMediumTableList($value, "", 1, $fieldValue);
								if(is_array($res[$i][$fieldName]))
									$res[$i][$fieldName]	= $res[$i][$fieldName];
							}
						break;
					*/
					}
				}
			}
		}
		return $res;
	}
	
	function getFilterFieldViewData()
	{
		$returnValue = array();
		foreach($this->_arrayField as $i=>$v)
		{
			if($v[$this->ARRAY_INDEX_FIELD_FILTER])
			{
				
				$returnValue[$i]['text']			= $v[$this->ARRAY_INDEX_FIELD_TEXT];
				$returnValue[$i]['name']			= $v[$this->ARRAY_INDEX_FIELD_NAME];
				$returnValue[$i]['edit']			= $v[$this->ARRAY_INDEX_FIELD_EDIT];
				$returnValue[$i]['input']			= $this->INPUT_TYPE_STRING[$v[$this->ARRAY_INDEX_FIELD_INPUT][0]];
				$returnValue[$i]['source_value']	= $this->getSourceValue($v[$this->ARRAY_INDEX_FIELD_INPUT][0]
														, $v[$this->ARRAY_INDEX_FIELD_SOURCE]);
			}
		}
		return $returnValue;
	}
	
	function getFieldsSettingViewSubData()
	{
		$returnValue = array();
		foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $i=>$f)
		{
			$returnValue[$i]['input']		= $this->INPUT_TYPE_STRING[$f[$this->ARRAY_SUB_INPUT_INDEX_FIELD_INPUT][0]];
			$returnValue[$i]['text']		= $f[$this->ARRAY_SUB_INPUT_INDEX_FIELD_TEXT];
			$returnValue[$i]['name']		= $f[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME];
		}
		return $returnValue;
	}
	
	function getFieldsSettingViewCrossData()
	{
		$returnValue = array();
		foreach($this->_arrayField as $i=>$f)
		{
			$returnValue[$i]['input']		= $this->INPUT_TYPE_STRING[$f[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0]];
			$returnValue[$i]['text']		= $f[$this->ARRAY_INDEX_FIELD_CROSS_TEXT];
			$returnValue[$i]['name']		= "fdt";
			$returnValue[$i]['idvalue']		= $f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE];
		}
		return $returnValue;
	}
	function getFieldsSettingViewData()
	{
		$returnValue = array();
		foreach($this->_arrayField as $i=>$f)
		{
			$returnValue[$i]['input']		= $this->INPUT_TYPE_STRING[$f[$this->ARRAY_INDEX_FIELD_INPUT][0]];
			$returnValue[$i]['view']		= $f[$this->ARRAY_INDEX_FIELD_VIEW];
			$returnValue[$i]['edit']		= $f[$this->ARRAY_INDEX_FIELD_EDIT];
			$returnValue[$i]['text']		= $f[$this->ARRAY_INDEX_FIELD_TEXT];
			$returnValue[$i]['name']		= $f[$this->ARRAY_INDEX_FIELD_NAME];
			$returnValue[$i]['specific']	= array();
			switch($returnValue[$i]['input'])
			{
				case "image":
				case "multiple_value_image":
					$returnValue[$i]['specific'][0]	= $f[$this->ARRAY_INDEX_FIELD_VALIDATION][2][0]['initial'];
					if(is_array($f[$this->ARRAY_INDEX_FIELD_VALIDATION][2]))
					foreach($f[$this->ARRAY_INDEX_FIELD_VALIDATION][2] as $s)
					{
						if(isset($s['thumb_admin']) && ($s['thumb_admin']))
							$returnValue[$i]['specific'][0]	= $s['initial'];
					}
				break;
				
			}
		}
		return $returnValue;
	}

	function getSourceValue($idValue, $source)
	{
		$returnValue = "";
		switch($idValue)
		{
			case $this->INPUT_TYPE_HIDDEN:
			case $this->INPUT_TYPE_TEXT:
			case $this->INPUT_TYPE_TEXT_PRIMARY;
			case $this->INPUT_TYPE_IMAGE:
			case $this->INPUT_TYPE_TEXT:
			case $this->INPUT_TYPE_MEDIUM_TABLE:
				$returnValue = "";
			break;
			case $this->INPUT_TYPE_CHECKBOX:
			case $this->INPUT_TYPE_RADIO:
			case $this->INPUT_TYPE_SELECT:
				$returnValue = $source;
			break;
			case $this->INPUT_TYPE_SIMPLE_TABLE:
				$returnValue = $this->getSimpleTableList($source);
			break;
		}
		return $returnValue;
	}
	
	function insertModule()
	{
		$idInsert	= $this->uri->segment(6);
		$recEdit	= "";
		$this->_arrayReturnValue["idEdit"] = $idInsert;
		$this->_arrayReturnValue["setting_insert"] = $this->_arraySetting[$this->ARRAY_INDEX_SETTING_INSERT];
		$this->_arrayReturnValue["setting_edit"] = $this->_arraySetting[$this->ARRAY_INDEX_SETTING_EDIT];
		$this->_arrayReturnValue["update_success"] = true;
		if($idInsert!="")
		{
			$this->_arrayReturnValue["update_success"] = false;
			$fieldsSetting	= $this->getFieldsSettingViewData();
			$recEdit = $this->getRecordsViewData(0, 1, $fieldsSetting, $idInsert);
			$this->printArr($recEdit, false);
			// Pasang ke realValue
			if($recEdit[1]!=0)
			foreach($this->_arrayField as $i=>$af)
			{	
				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= "";
				switch($fieldsSetting[$i]['input'])
				{
					case "hidden":
					case "text":
					case "text_primary";
					case "checkbox":
					case "radio":
					case "multiple_value":
					case "text_area":
					case "text_area_plus_editor":
					case "multiple_value_foreign":
//						echo $fieldsSetting[$i]['input'];
						$this->printArr($recEdit[0][0][$fieldsSetting[$i]['name']], 0);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $recEdit[0][0][$fieldsSetting[$i]['name']];
					break;
					case "password":
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $this->pass_word->decryptPass($recEdit[0][0][$fieldsSetting[$i]['name']]);
					break;
					case "select":
						$valueSelect = $recEdit[0][0][$fieldsSetting[$i]['name']];
						if(count($valueSelect)>0)
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $valueSelect[0];
					break;
					case "simple_table":
						$valueSimple = $recEdit[0][0][$fieldsSetting[$i]['name']];
						if(count($valueSimple)>0)
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $valueSimple['idRec'];
					break;
					case "image":
					case "file":
					case "multiple_value_image":
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][0]	= $recEdit[0][0][$fieldsSetting[$i]['name']];
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2]	= "";
					break;
					case "medium_table":
					case "advance_table":
						$vv 			= $recEdit[0][0][$fieldsSetting[$i]['name']];
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= array($vv[2], $vv);
					break;
					default:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= "";
					break;
				}
			}
		}
		$this->printArr($this->_arrayField, false);
		$post_value	= $this->input->post('init');
		if(isset($post_value) && ($this->input->post('init')=='save'))
		{
			$msg					= "";
			$insertValid			= true;
			$returnValueContainer	= array();
			foreach($this->_arrayField as $i=>$af)
			{	
				$moduleName	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_NAME];
				$moduleText	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_TEXT];
//				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= ""; 
				switch($af[$this->ARRAY_INDEX_FIELD_INPUT][0])
				{
					case $this->INPUT_TYPE_HIDDEN:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";

						$post_value	= $this->input->post($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_NAME]);

						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= 
						($post_value=="")?$realValue:$post_value;
					break;
					case $this->INPUT_TYPE_TEXT:
					case $this->INPUT_TYPE_TEXT_PRIMARY:
					case $this->INPUT_TYPE_PASSWORD:
					case $this->INPUT_TYPE_TEXT_AREA:
					case $this->INPUT_TYPE_TEXT_AREA_PLUS_EDITOR:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
						$post_value	= $this->input->post($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_NAME]);

						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
					break;
					case $this->INPUT_TYPE_CHECKBOX:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$af[$this->ARRAY_INDEX_FIELD_SOURCE]; // GetValueFromTable
						$post_value 		= array();
						$iFound				= 0;
						$countSourceValue	= count($af[$this->ARRAY_INDEX_FIELD_SOURCE]);
						for($o = 0; $o < $countSourceValue; $o++)
						{
							$tempPostValue = $this->input->post("input-".$moduleName."-".$o);
							if(isset($tempPostValue) && ($tempPostValue!=''))
							{
								$post_value[$iFound] = $tempPostValue;
								$iFound++;
							}
						}
						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);

						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $post_value;
					break;
					case $this->INPUT_TYPE_RADIO:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]; // GetValueFromTable
						$post_value 		= $this->input->post($moduleName);
						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
					break;
					case $this->INPUT_TYPE_IMAGE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
//						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE] = "";
						$post_value 			= $_FILES[$moduleName]['name'];
						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]	= $post_value;
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2]	= false;
						// Check delete parameter
						$tempDeleteImage = $this->input->post("input-image-".$moduleName);
						if(isset($tempDeleteImage) && ($tempDeleteImage!=''))
						{
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2]	= true;
						}
						
					break;
					case $this->INPUT_TYPE_FILE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
//						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE] = "";
						$post_value 			= $_FILES[$moduleName]['name'];
						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]	= $post_value;
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2]	= false;
						// Check delete parameter
						$tempDeleteImage = $this->input->post("input-file-".$moduleName);
						if(isset($tempDeleteImage) && ($tempDeleteImage!=''))
						{
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2]	= true;
						}
						
					break;
					case $this->INPUT_TYPE_SELECT:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]; // GetValueFromTable
						$post_value 		= $this->input->post($moduleName);
						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $post_value;
					break;
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->getSimpleTableList($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]);	// GetValueFromTable
						$post_value 			= $this->input->post($moduleName);
						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$realValue = ($post_value!=$realValue)?$post_value:$realValue;
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $realValue;
					break;
					case $this->INPUT_TYPE_MEDIUM_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]; // GetValueFromTable
						$post_value 		= $this->input->post($moduleName);
						$detail_value		= $this->getMediumTableList(
																	$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]	
																	, "", 1, $post_value);
						
						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						$post_value	= array($post_value, $detail_value);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
					break;
					case $this->INPUT_TYPE_ADVANCE_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
							$this->getAdvanceTableFilterSource($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE][4]);
						
						$post_value 		= $this->input->post($moduleName);
						$detail_value		= $this->getAdvanceTableList(
																	$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]	
																	, 1, $post_value);
						$returnValueContainer	= $this->checkValidation(
							$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
							, $post_value
							, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						$post_value	= array($post_value, $detail_value);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= ($post_value!=$realValue)?$post_value:$realValue;
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
						$post_value 		= array();
						$check_value 		= array();
						$iFound				= 0;
						$countSourceValue	= count($_FILES);
						for($o = 0; $o < $countSourceValue; $o++)
						{
							if(isset($_FILES["input-".$moduleName."-".$o]['name']))
							{
								$tempPostValue = $_FILES["input-".$moduleName."-".$o]['name'];
								if(isset($tempPostValue) && ($tempPostValue!=''))
								{
									$post_value[$iFound][0] = $tempPostValue;
									$check_value[$iFound] = $tempPostValue;
									$post_value[$iFound][1] = "input-".$moduleName."-".$o;
									$iFound++;
								}
							}
						}

						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $check_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]	= $post_value;
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2]	= "";
						if($idInsert!="")
						{
							$multipleImageData	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][0];
							if(isset($multipleImageData))
							{
								$countMultipleImage	= count($multipleImageData);
								$iFound = 0;
								for($o = 0; $o < $countMultipleImage; $o++)
								{
									$tempPostValue = $this->input->post("input-multiple-image-".$moduleName."-".$o);
									if(isset($tempPostValue) && ($tempPostValue!=''))
									{
										$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE][2][$iFound]	= $tempPostValue;
										$iFound++;
									}
								}
							}
						}
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] 	= "";
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]		= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_INPUT][0]);
						$post_value 		= array();
						$iFound				= 0;
						$countSourceValue	= count($this->input->post());
						for($o = 0; $o < $countSourceValue; $o++)
						{
							$postInput = $this->input->post("input-".$moduleName."-".$o);
							if($postInput!='')
							{
								$tempPostValue = $this->input->post("input-".$moduleName."-".$o);
								if(isset($tempPostValue) && ($tempPostValue!=''))
								{
									$post_value[$iFound] = $tempPostValue;
									$iFound++;
								}
							}
						}

						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $post_value;
						$this->printArr($post_value,0);
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] 	= "";
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]		= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_INPUT][0]);
						$post_value 		= array();
						$iFound				= 0;
						$countSourceValue	= count($this->input->post());
						for($o = 0; $o < $countSourceValue; $o++)
						{
							$postId 	= $this->input->post("id-".$moduleName."-".$o);
							$postInput 	= $this->input->post("input-".$moduleName."-".$o);
							if($postInput!='')
							{
								$tempPostid 	= $this->input->post("id-".$moduleName."-".$o);
								$tempPostValue 	= $this->input->post("input-".$moduleName."-".$o);
								if(isset($tempPostValue) && ($tempPostValue!=''))
								{
									$post_value[$iFound][0] = $tempPostid;
									$post_value[$iFound][1] = $tempPostValue;
									$iFound++;
								}
							}
						}

						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $post_value;
					break;
				} // switch value type
			} // foreach value
			if($insertValid)
			{
				$this->_arrayReturnValue["update_success"] = true;
				$this->updateData($this->_arrayField);
				foreach($this->_arrayField as $i=>$af)
				{
					if($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_INPUT][0]!=$this->INPUT_TYPE_HIDDEN)
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= "";
				}
				$this->_arrayReturnValue["idEdit"] = "";
				$this->_arrayReturnValue["msg"] = array(array("Input data sukses.",""));
			}
			else
			{
				$this->_arrayReturnValue["msg"] = $msg;
			}
		}
		else
		{
			foreach($this->_arrayField as $i=>$af)
			{
				switch($af[$this->ARRAY_INDEX_FIELD_INPUT][0])
				{
					case $this->INPUT_TYPE_HIDDEN:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
					break;
					case $this->INPUT_TYPE_TEXT:
					case $this->INPUT_TYPE_TEXT_PRIMARY:
					case $this->INPUT_TYPE_PASSWORD:
					case $this->INPUT_TYPE_TEXT_AREA:
					case $this->INPUT_TYPE_TEXT_AREA_PLUS_EDITOR:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
					break;
					case $this->INPUT_TYPE_CHECKBOX:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE];							// GetValueFromTable
					break;
					case $this->INPUT_TYPE_RADIO:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE];							// GetValueFromTable
					break;
					case $this->INPUT_TYPE_IMAGE:
					case $this->INPUT_TYPE_FILE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
					break;
					case $this->INPUT_TYPE_SELECT:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE];							// GetValueFromTable
					break;
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
						$this->getSimpleTableList($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]);	// GetValueFromTable
					break;
					case $this->INPUT_TYPE_MEDIUM_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
					break;
					case $this->INPUT_TYPE_ADVANCE_TABLE:
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = 
							$this->getAdvanceTableFilterSource($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE][4]);
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
					break;
					case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
					case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE_VALUE] = "";
					break;
				} // switch value type
				if($idInsert=="")
				{
					$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
//					echo $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]."<br>";
				}
			} // foreach value
		} // Default value insert
	} // module insertModule();
	
	function insertCrossModule()
	{
		$idInsert	= $this->uri->segment(6);
		$recEdit	= "";
		$this->_arrayReturnValue["idEdit"] 	= $idInsert;
		$this->_arrayReturnValue["msg"]		= "";
		$this->_arrayReturnValue["update_success"] = false;
		if($idInsert!="")
		{
			$fieldsSetting	= $this->getFieldsSettingViewCrossData();
			$recEdit = $this->getRecordsViewCrossData($idInsert);
			$this->printArr($recEdit, false);
			// Pasang ke realValue
			if($recEdit[0]['fid']==$idInsert)
			foreach($this->_arrayField as $i=>$af)
			{	
				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= "";
				$fieldDataValue	= $recEdit[0]['fdt'];
//				echo $recEdit[0]['fid']." - ".$af[$this->ARRAY_INDEX_FIELD_CROSS_VALUE]."<br>";
				if($recEdit[0]['fid']==$af[$this->ARRAY_INDEX_FIELD_CROSS_VALUE])
				switch($fieldsSetting[$i]['input'])
				{
					case "text":
					case "radio":
					case "text_area":
//						echo $fieldsSetting[$i]['input'];
						$this->printArr($recEdit[0]['fdt'],0);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $fieldDataValue;
					break;
					case "select":
						$valueSelect = $fieldDataValue;
						if(count($valueSelect)>0)
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $valueSelect[0];
					break;
					case "simple_table":
						$valueSimple = $fieldDataValue;
						if(count($valueSimple)>0)
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $valueSimple['idRec'];
					break;
					case "image":
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][0]	= $fieldDataValue;
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][2]	= "";
					break;
					default:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= "";
					break;
				}
			}
		}
		$this->printArr($this->_arrayField, false);
		$post_value	= $this->input->post('init');
		if(isset($post_value) && ($this->input->post('init')=='save'))
		{
			$msg					= "";
			$insertValid			= true;
			$returnValueContainer	= array();
			foreach($this->_arrayField as $i=>$af)
			{	
//				$moduleName	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_NAME];
				$inputName	= "fdt";
				$moduleText	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_TEXT];
				if($recEdit[0]['fid']==$af[$this->ARRAY_INDEX_FIELD_CROSS_VALUE])
				switch($af[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
				{
					case $this->INPUT_TYPE_TEXT:
					case $this->INPUT_TYPE_TEXT_AREA:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = "";
						$post_value	= $this->input->post($inputName);
						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $post_value;
					break;
					case $this->INPUT_TYPE_RADIO:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_SOURCE]; // GetValueFromTable
						$post_value 		= $this->input->post($inputName);
						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $post_value;
					break;
					case $this->INPUT_TYPE_IMAGE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = "";
//						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE] = "";
						$post_value 			= $_FILES[$inputName]['name'];
						$returnValueContainer	= $this->checkValidation(
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION]
						, $post_value
						, $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$realValue	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][1]	= $post_value;
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][2]	= false;
						// Check delete parameter
						$tempDeleteImage = $this->input->post("input-image-".$inputName);
						if(isset($tempDeleteImage) && ($tempDeleteImage!=''))
						{
							$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][2]	= true;
						}
						
					break;
					case $this->INPUT_TYPE_SELECT:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_SOURCE]; // GetValueFromTable
						$post_value 		= $this->input->post($inputName);
						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_CROSS_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $post_value;
					break;
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = 
						$this->getSimpleTableList($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_SOURCE]);	// GetValueFromTable
						$post_value 			= $this->input->post($inputName);
						$returnValueContainer	= $this->checkValidation(
						$af[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION]
						, $post_value
						, $af[$this->ARRAY_INDEX_FIELD_CROSS_TEXT]
						);
						if(!$returnValueContainer['valid'])
						{
							$insertValid = false;
							$msg[(is_array($msg))?count($msg):0] = array($returnValueContainer['msg'],$moduleName);
						}
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= $post_value;
					break;
				} // switch value type
			} // foreach value
			if($insertValid)
			{
				$this->_arrayReturnValue["update_success"] = true;
				$this->updateDataCross($this->_arrayField);
				foreach($this->_arrayField as $i=>$af)
				{
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= "";
				}
				$this->_arrayReturnValue["msg"] = array(array("sukses",""));
			}
			else
			{
				$this->_arrayReturnValue["msg"] = $msg;
			}
		}
		else
		{
			foreach($this->_arrayField as $i=>$af)
			{
//				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]	= "";
				switch($af[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
				{
					case $this->INPUT_TYPE_TEXT:
					case $this->INPUT_TYPE_TEXT_AREA:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = "";
					break;
					case $this->INPUT_TYPE_RADIO:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_SOURCE];							// GetValueFromTable
					break;
					case $this->INPUT_TYPE_IMAGE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = "";
					break;
					case $this->INPUT_TYPE_SELECT:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = 
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_SOURCE];							// GetValueFromTable
					break;
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE] = 
						$this->getSimpleTableList($this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_CROSS_SOURCE]);	// GetValueFromTable
					break;
				} // switch value type
				if($idInsert=="")
				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_REAL_VALUE]	= $this->fillInput($af[$this->ARRAY_INDEX_FIELD_VALUE]);
			} // foreach value
		} // Default value insert
	} // module insertModule();

	function checkPrimary()
	{
		$moduleName		= $this->uri->segment(6);
		$text			= $this->uri->segment(7);
		$tableName		= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$fieldKey		= $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_NAME];
		$sql			= "SELECT `$fieldKey` FROM `$tableName` WHERE `$fieldKey` = '$text'";
		$rec			= $this->db->query($sql);
		if($rec->num_rows()>0)
			return "no";
		return "yes";
	}
	
	function singleEditModule()
	{
		// single_edit/1/Agoes Tri hariyanto/nama/
		$idRec		= $this->uri->segment(6);
		$fieldIdValue = $idRec;
		$fldEdt		= $this->uri->segment(7);
		$fldVal		= $this->uri->segment(8);
		$fldVal		= rawurldecode($fldVal);
		$table_name 	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$fieldId		= $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_NAME];
		$field			= "";
		$returnData['input']		= "";
		$returnData['interface']	= "";
		$returnData['value']		= $fldVal;
		$returnData['fieldname']	= $fldEdt;
		$returnData['fieldidvalue']	= $idRec;
		$returnData['msg']			= "";
		$insertValid = true;
		foreach($this->_arrayField as $sif)
		{
			if($sif[$this->ARRAY_INDEX_FIELD_NAME] == $fldEdt)
			{
				$returnData['input']	= $this->INPUT_TYPE_STRING[$sif[$this->ARRAY_INDEX_FIELD_INPUT][0]];
				$returnData['interface']	= "edit";
				if ($fldVal!="")
				{
					$returnValueContainer	= $this->checkValidation(
					$sif[$this->ARRAY_INDEX_FIELD_VALIDATION]
					, $fldVal
					, $sif[$this->ARRAY_INDEX_FIELD_TEXT]
					);
					$this->printArr($returnValueContainer, false);
					if((!$returnValueContainer['valid']) || ($returnValueContainer['valid']==''))
					{
						$insertValid = false;
						$returnData['msg'] = array($returnValueContainer['msg'],$fldEdt);
					}
					if($insertValid)
					{
						$this->updateSpecificFieldFromTable($table_name, $fldEdt, $fldVal, $fieldId, $fieldIdValue);
						$returnData['interface']	= "view";
					}
				}
				$returnData['value']		= $this->getFieldValueFromRecord($table_name, $fldEdt, $fieldId, $fieldIdValue);
				$inputType					= $sif[$this->ARRAY_INDEX_FIELD_INPUT][0];
				$fieldSource				= $sif[$this->ARRAY_INDEX_FIELD_SOURCE];
				$returnData['value'] 		= $this->convertGeneralValtoInputVal($returnData['value'], $inputType, $fieldSource);
				$returnData['source']		= "";
				$returnData['attr']			= "";
				switch($sif[$this->ARRAY_INDEX_FIELD_INPUT][0])
				{
					case $this->INPUT_TYPE_TEXT:
						$maxlength	= $sif[$this->ARRAY_INDEX_FIELD_INPUT][1]['MAX_LENGTH'];
						$maxlength	= ($maxlength>0)?$maxlength:50;
						$size		= $sif[$this->ARRAY_INDEX_FIELD_INPUT][1]['SIZE'];
						switch($size)
						{
							case 0:
								$size = 20;
							break;
							case 1:
								$size = 40;
							break;
							case 2:
								$size = 80;
							break;
						}
	
						$returnData['attr']	= array(
													"maxlength"	=> $maxlength,
													"size"		=> $size,
													);
					case $this->INPUT_TYPE_TEXT_AREA:
						if(isset($sif[$this->ARRAY_INDEX_FIELD_INPUT][1]['ROWS']))
						{
							$rows		= $sif[$this->ARRAY_INDEX_FIELD_INPUT][1]['ROWS'];
							$cols		= $sif[$this->ARRAY_INDEX_FIELD_INPUT][1]['COLS'];
							switch($cols)
							{
								case 0:
									$cols = 20;
								break;
								case 1:
									$cols = 40;
								break;
								case 2:
									$cols = 80;
								break;
							}
		
							$returnData['attr']	= array(
														"cols"		=> $cols,
														"rows"		=> $rows,
														);
						}
					break;
					case $this->INPUT_TYPE_RADIO:
					case $this->INPUT_TYPE_SELECT:
					case $this->INPUT_TYPE_MEDIUM_TABLE:
						$returnData['source'] = 
						$sif[$this->ARRAY_INDEX_FIELD_SOURCE]; 
					break;
					case $this->INPUT_TYPE_SIMPLE_TABLE:
						$returnData['source'] =
						$this->getSimpleTableList($sif[$this->ARRAY_INDEX_FIELD_SOURCE]);
					break;
					case $this->INPUT_TYPE_ADVANCE_TABLE:
						$returnData['source'] = 
						$this->getAdvanceTableFilterSource($sif[$this->ARRAY_INDEX_FIELD_SOURCE][4]);
					break;
				}
			}
		}
		$returnData['msg']				= $this->subInputEditData($table_name, $fieldId, $fieldIdValue, $field, $fldVal);
		return $returnData;
	}
	
	function mediumTableModule()
	{
		$moduleName		= $this->uri->segment(6);
		$found			= false;
		if(strpos($moduleName, '-')>-1)
		$moduleName		= substr($moduleName, 0, strpos($moduleName, '-'));
//		echo $moduleName;
		$paramSearch	= $this->uri->segment(7);
		$paramSearch	= rawurldecode($paramSearch);
		
		foreach($this->_arrayField as $i=>$val)
			if($val[$this->ARRAY_INDEX_FIELD_NAME] == $moduleName)
			{
				$found = true;
				return $this->getMediumTableList(
				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]
				, $paramSearch);	// GetValueFromTable
			}
		if(!$found)
		{
			foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $i=>$val)
				if($val[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME] == $moduleName)
				{
					$this->printArr($val[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE], false);
					return $this->getMediumTableList(
						$val[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]
						, $paramSearch);	// GetValueFromTable
				}
		}
	}
	
	function advanceTableModule()
	{
		$moduleName		= $this->uri->segment(6);
		$found			= false;
		if(strpos($moduleName, '-')>-1)
			$moduleName		= substr($moduleName, 0, strpos($moduleName, '-'));
		foreach($this->_arrayField as $i=>$val)
			if($val[$this->ARRAY_INDEX_FIELD_NAME] == $moduleName)
			{
				$found = true;
				return $this->getAdvanceTableList(
				$this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE]);	// GetValueFromTable
			}
		if(!$found)
		{
			foreach($this->_arraySubInput[$this->ARRAY_INDEX_SUB_INPUT_FIELDS] as $i=>$val)
				if($val[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NAME] == $moduleName)
				{
					$this->printArr($val[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE], false);
					return $this->getAdvanceTableList(
						$val[$this->ARRAY_SUB_INPUT_INDEX_FIELD_SOURCE]);	// GetValueFromTable
				}
		}
	}

	
	function multipleValueModule()
	{
		$moduleName		= $this->uri->segment(6);
		foreach($this->_arrayField as $i=>$val)
		{
			if($val[$this->ARRAY_INDEX_FIELD_NAME] == $moduleName)
			{
				$arrayField = array();
				$arrayField[0] 	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE][1];
				$arrayField[1] 	= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE][2];
				$tableName		= $this->_arrayField[$i][$this->ARRAY_INDEX_FIELD_SOURCE][0];
				$arrayField[2] 	= $tableName;
				return $this->getMultipleValueList($arrayField);	// GetValueFromTable
			}
		}
	}

	function fillInput($valueType)
	{
		if(!is_array($valueType))
			switch($valueType)
			{
				case $this->FIELD_VALUE_IP:
					return $_SERVER['REMOTE_ADDR'];
				break;
				case $this->FIELD_VALUE_TIME:
					return date("h:i:s");
				break;
				case $this->FIELD_VALUE_DATE:
					return date("Y/m/d");
				break;
				case $this->FIELD_VALUE_USER:
					return $this->_arraySetting[$this->ARRAY_INDEX_SETTING_ID_USER];
				break;
				case $this->FIELD_VALUE_AUTO:
				case $this->FIELD_VALUE_DEFAULT:
				default:
					return "";
				break;
			}
		else
			return $valueType[0];
	}
	
	function convertToTextIndex(array $arrayField, $subInputMode = false)
	{
		$valueReturn = array();
		foreach($arrayField as $key => $aF)
		{
			$valueReturn[$key]['name']			= $aF[$this->ARRAY_INDEX_FIELD_NAME];
			$valueReturn[$key]['text']			= $aF[$this->ARRAY_INDEX_FIELD_TEXT];
			$valueReturn[$key]['attr']			= "";
			
			switch($aF[$this->ARRAY_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_HIDDEN:
					$valueReturn[$key]['input'] = "hidden";
				break;
				case $this->INPUT_TYPE_TEXT:
					$valueReturn[$key]['input'] = "text";
					$maxlength	= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['MAX_LENGTH'];
					$maxlength	= ($maxlength>0)?$maxlength:50;
					$size		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"maxlength"	=> $maxlength,
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_TEXT_PRIMARY:
					$valueReturn[$key]['input'] = "text_primary";
					$maxlength	= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['MAX_LENGTH'];
					$maxlength	= ($maxlength>0)?$maxlength:50;
					$size		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"maxlength"	=> $maxlength,
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_PASSWORD:
					$valueReturn[$key]['input'] = "password";
					$maxlength	= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['MAX_LENGTH'];
					$maxlength	= ($maxlength>0)?$maxlength:50;
					$size		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"maxlength"	=> $maxlength,
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_TEXT_AREA:
					$valueReturn[$key]['input'] = "text_area";
					$rows		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['ROWS'];
					$cols		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['COLS'];
					switch($cols)
					{
						case 0:
							$cols = 20;
						break;
						case 1:
							$cols = 40;
						break;
						case 2:
							$cols = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"cols"		=> $cols,
														"rows"		=> $rows,
													);
				break;
				case $this->INPUT_TYPE_TEXT_AREA_PLUS_EDITOR:
					$valueReturn[$key]['input'] = "text_area_plus_editor";
					$rows		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['ROWS'];
					$cols		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['COLS'];
					switch($cols)
					{
						case 0:
							$cols = 20;
						break;
						case 1:
							$cols = 40;
						break;
						case 2:
							$cols = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"cols"		=> $cols,
														"rows"		=> $rows,
													);
				break;
				case $this->INPUT_TYPE_RADIO:
					$valueReturn[$key]['input'] = "radio";
				break;
				case $this->INPUT_TYPE_CHECKBOX:
					$valueReturn[$key]['input'] = "checkbox";
				break;
				case $this->INPUT_TYPE_PASSWORD:
					$valueReturn[$key]['input'] = "password";
				break;
				case $this->INPUT_TYPE_IMAGE:
					$valueReturn[$key]['input'] = "image";
					$size		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}
					$valueReturn[$key]['attr']	= array(
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_FILE:
					$valueReturn[$key]['input'] = "file";
					$size		= $aF[$this->ARRAY_INDEX_FIELD_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}
					$valueReturn[$key]['attr']	= array(
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_SELECT:
					$valueReturn[$key]['input'] = "select";
				break;
				case $this->INPUT_TYPE_SIMPLE_TABLE:
					$valueReturn[$key]['input'] = "simple_table";
				break;
				case $this->INPUT_TYPE_MEDIUM_TABLE:
					$valueReturn[$key]['input'] = "medium_table";
				break;
				case $this->INPUT_TYPE_ADVANCE_TABLE:
					$valueReturn[$key]['input'] = "advance_table";
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
					$valueReturn[$key]['input'] = "multiple_value_table";
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
					$valueReturn[$key]['input'] = "multiple_value_table_foreign";
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
					$valueReturn[$key]['input'] = "multiple_value_image";
				break;
			}
			if(!$subInputMode)
			{
				$valueReturn[$key]['view']			= $aF[$this->ARRAY_INDEX_FIELD_VIEW];
	
				$valueReturn[$key]['edit']			= $aF[$this->ARRAY_INDEX_FIELD_EDIT];
				
				$valueReturn[$key]['note']			= (isset($aF[$this->ARRAY_INDEX_FIELD_NOTE]))?$aF[$this->ARRAY_INDEX_FIELD_NOTE]:"";
				
				$valueReturn[$key]['real_value']	= $aF[$this->ARRAY_INDEX_FIELD_REAL_VALUE];
				
				$valueReturn[$key]['real_source']	= $aF[$this->ARRAY_INDEX_FIELD_SOURCE_VALUE];
			}
			else
			{
				if(isset($aF[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NOTE]))
					$valueReturn[$key]['note']			= $aF[$this->ARRAY_SUB_INPUT_INDEX_FIELD_NOTE];
				else
					$valueReturn[$key]['note']			= "";
				$valueReturn[$key]['real_source']	= $aF[$this->ARRAY_SUB_INPUT_INDEX_FIELD_REAL_SOURCE];
				$valueReturn[$key]['post_value']	= $aF[$this->ARRAY_SUB_INPUT_INDEX_FIELD_POST_VALUE];
			}
		}
		return $valueReturn;
	}
	
	function convertToTextIndexCross(array $arrayField)
	{
		$valueReturn = array();
		foreach($arrayField as $key => $aF)
		{
			$valueReturn[$key]['name']			= 'fdt';
			$valueReturn[$key]['text']			= $aF[$this->ARRAY_INDEX_FIELD_CROSS_TEXT];
			$valueReturn[$key]['value']			= $aF[$this->ARRAY_INDEX_FIELD_CROSS_VALUE];
			$valueReturn[$key]['attr']			= "";
			
			$this->printArr($aF, 0);
			
			switch($aF[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
			{
				case $this->INPUT_TYPE_TEXT:
					$valueReturn[$key]['input'] = "text";
					$maxlength	= $aF[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][1]['MAX_LENGTH'];
					$maxlength	= ($maxlength>0)?$maxlength:50;
					$size		= $aF[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"maxlength"	=> $maxlength,
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_TEXT_AREA:
					$valueReturn[$key]['input'] = "text_area";
					$rows		= $aF[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][1]['ROWS'];
					$cols		= $aF[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][1]['COLS'];
					switch($cols)
					{
						case 0:
							$cols = 20;
						break;
						case 1:
							$cols = 40;
						break;
						case 2:
							$cols = 80;
						break;
					}

					$valueReturn[$key]['attr']	= array(
														"cols"		=> $cols,
														"rows"		=> $rows,
													);
				break;
				case $this->INPUT_TYPE_RADIO:
					$valueReturn[$key]['input'] = "radio";
				break;
				case $this->INPUT_TYPE_IMAGE:
					$valueReturn[$key]['input'] = "image";
					$size		= $aF[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][1]['SIZE'];
					switch($size)
					{
						case 0:
							$size = 20;
						break;
						case 1:
							$size = 40;
						break;
						case 2:
							$size = 80;
						break;
					}
					$valueReturn[$key]['attr']	= array(
														"size"		=> $size,
													);
				break;
				case $this->INPUT_TYPE_SELECT:
					$valueReturn[$key]['input'] = "select";
				break;
				case $this->INPUT_TYPE_SIMPLE_TABLE:
					$valueReturn[$key]['input'] = "simple_table";
				break;
			}
			$valueReturn[$key]['note']			= (isset($aF[$this->ARRAY_INDEX_FIELD_CROSS_NOTE]))?$aF[$this->ARRAY_INDEX_FIELD_CROSS_NOTE]:"";
			if(isset($aF[$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE]))
			$valueReturn[$key]['real_value']	= $aF[$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE];
			if(isset($aF[$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE]))
			$valueReturn[$key]['real_source']	= $aF[$this->ARRAY_INDEX_FIELD_CROSS_REAL_SOURCE];
		}
		return $valueReturn;
	}
	
	function getMultipleValueList($arrayFieldSource)
	{
		$paramSearch		= $this->uri->segment(7);
		$sql		= "SELECT ".$arrayFieldSource[1]." idRec, ".$arrayFieldSource[0][0]." dataRec FROM ".$arrayFieldSource[2]
					." WHERE ".$arrayFieldSource[0][0]." like '%".$paramSearch."%' limit 5";
		$record		= $this->db->query($sql);
		$resource	= $record->result_array();
		return array ($resource, array("ID", $arrayFieldSource[0][1]));
	}
	
	function getMultipleValueData($arrayFieldSource, $id, $foreign = false)
	{
		$srcTableName	= $arrayFieldSource[0];
		$srcFieldName	= $arrayFieldSource[1];
		$srcKeyName		= $arrayFieldSource[2];
		$tableName		= $arrayFieldSource[3];
		$keyName		= $arrayFieldSource[4];
		$dataName		= $arrayFieldSource[5];
		$foreignName	= $arrayFieldSource[6];
		if($foreign)
		$sql		= "SELECT $tableName.$dataName idRec, ".$srcTableName.".".$srcFieldName[0]." dataRec FROM $tableName"
					." INNER JOIN $srcTableName ON $tableName.$dataName = $srcTableName.$srcKeyName"
					." WHERE $tableName.$foreignName = '$id'";
		else
		$sql		= "SELECT $dataName dataRec FROM $tableName"
					." WHERE $foreignName = '$id'";
		$record		= $this->db->query($sql);
		$resource	= $record->result_array();
		$this->printArr($resource, 0);
		return $resource;
	}
	
	function getMultipleValueImage($arrayFieldSource, $id)
	{
		$srcTableName	= $arrayFieldSource[0];
		$srcKeyName		= $arrayFieldSource[1];
		$srcForeignName	= $arrayFieldSource[2];
		$srcDataName	= $arrayFieldSource[3];
		$sql		= "SELECT `$srcKeyName` idRec, `$srcDataName` dataRec FROM `$srcTableName`"
					." WHERE $srcForeignName = '$id'";
		$record		= $this->db->query($sql);
		$resource	= $record->result_array();
		$this->printArr($resource, 0);
		if($record->num_rows()==0)
			return;
		return $resource;
	}
	
	function getSimpleTableList($arrayFieldSource, $id="")
	{
		$this->printArr($arrayFieldSource, false);
		$whereSql		= "";
		if(isset($arrayFieldSource[3]))
		{
			$whereData		= $arrayFieldSource[3];
			if(count($whereData)>0)
			foreach($whereData as $i=>$where)
			{
				$fieldWhere	= $where[0];
				$valueWhere	= $where[2];
				$whereSql .= " AND $fieldWhere ";
				switch($where[1])
				{
					case $this->OPERATOR_EQUAL:
						$whereSql	.= "= '$valueWhere'";
					break;
					case $this->OPERATOR_NOT_EQUAL:
						$whereSql	.= "<> '$valueWhere'";
					break;
					case $this->OPERATOR_LESS_THAN:
						$whereSql	.= "< $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN:
						$whereSql	.= "> $valueWhere";
					break;
					case $this->OPERATOR_LESS_THAN_EQUAL:
						$whereSql	.= "<= $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN_EQUAL:
						$whereSql	.= ">= $valueWhere";
					break;
					case $this->OPERATOR_LIKE:
						$whereSql	.= "LIKE '%$valueWhere%'";
					break;
				}
			}
		}
		$sql		= "SELECT ".$arrayFieldSource[1]." idRec, ".$arrayFieldSource[0]." dataRec FROM ".$arrayFieldSource[2]
					." WHERE 1 = 1";
		if($id!="")
			$sql	.= " AND ".$arrayFieldSource[1]."='".$id."'";
		if($whereSql!="")
			$sql	.= $whereSql;
		$record		= $this->db->query($sql);
		$resource	= $record->result_array();
		return $resource;
	}
	
	function getAdvanceTableFilterSource($arrayFilterSource)
	{
		$returnArrayFilter = array();
		foreach($arrayFilterSource as $i => $filter)
		{
			$returnArrayFilter[$i]['text']	= $filter[0];
			$returnArrayFilter[$i]['name']	= $filter[0];
			$this->printArr($filter, false);
			switch($filter[3])
			{
				case $this->INPUT_TYPE_TEXT:
					$returnArrayFilter[$i]['input']		= "text";
					$returnArrayFilter[$i]['source']	= "";
				break;
				case $this->INPUT_TYPE_RADIO:
					$returnArrayFilter[$i]['input']		= "radio";
					$returnArrayFilter[$i]['source']	= $filter[4];
				break;
				case $this->INPUT_TYPE_SELECT:
					$returnArrayFilter[$i]['input']		= "select";
					$returnArrayFilter[$i]['source']	= $filter[4];
				break;
				case $this->INPUT_TYPE_SIMPLE_TABLE:
					$returnArrayFilter[$i]['input']		= "simple_table";
					$returnArrayFilter[$i]['source']	= $this->getSimpleTableList($filter[4]);	
				break;
			}
		}
		return $returnArrayFilter;
	}
	
	function getMediumTableList($arrayFieldSource, $paramSearch="", $count=10, $id="")
	{
		$tableName	= $arrayFieldSource[2];
		$IdName		= $arrayFieldSource[1];

		$paramSearch		= rawurldecode($paramSearch);
		$fieldsName	= $arrayFieldSource[0];
		$fieldSql	= "";
		$fieldWhereSql	= "";
		$textField		= array();
		$textField[0]	= "ID";
		$fieldReplace	= array();
		foreach($fieldsName as $i=>$field)
		{	
			$fieldSql .= ", ".$tableName[1].".".$field[0]." ".$field[1];
			if($field[2]==true)
			{
				if($i>0)
					$fieldWhereSql	.= "OR ";
				$fieldWhereSql		.= $tableName[1].".".$field[0]." like '%".$paramSearch."%' ";
			}
			$textField[count($textField)+1] = $field[3];
			$fieldReplace[$i]['name'] 		= ($field[1]=="")?$field[0]:$field[1];
			$fieldReplace[$i]['replace'] 	=  (isset($field[4]))?$field[4]:"";
		}
		
		$innerJoin		= $arrayFieldSource[3];
		$innerJoinSql	= "";
		if(count($innerJoin)>0)
		foreach($innerJoin as $i=>$inner)
		{
			$aliasInnerTable = (isset($inner[0][1]))?$inner[0][1]:$inner[0][0];
			$innerJoinSql .= " INNER JOIN ".$inner[0][0]." ".$aliasInnerTable
							." ON ".$inner[1][1].".".$inner[3]." = ".$aliasInnerTable.".".$inner[2];
			foreach($inner[4] as $i=>$field)
			{	
				$fieldSql .= ", ".$aliasInnerTable.".".$field[0]." ".$field[1];
				if($field[2]==true)
				{
					if($fieldWhereSql!="")
					$fieldWhereSql	.="OR ";
					$fieldWhereSql	.= $aliasInnerTable.".".$field[0]." like '%".$paramSearch."%' ";
				}
				$textField[count($textField)+1] = $field[3];
				$fieldReplace[count($fieldReplace)]['name']		= ($field[1]=="")?$field[0]:$field[1];
				$fieldReplace[count($fieldReplace)-1]['replace'] 	=  (isset($field[4]))?$field[4]:"";
			}
		}
		
		$whereSql		= "";
		if(isset($arrayFieldSource[4]))
		{
			$whereData		= $arrayFieldSource[4];
			if(count($whereData)>0)
			foreach($whereData as $i=>$where)
			{
				$aliasTable = (isset($where[0][1]))?$where[0][1]:$where[0][0];
				$fieldWhere	= $where[1];
				$valueWhere	= $where[3];
				$whereSql .= " AND $aliasTable.$fieldWhere ";
				switch($where[2])
				{
					case $this->OPERATOR_EQUAL:
						$whereSql	.= "= '$valueWhere'";
					break;
					case $this->OPERATOR_NOT_EQUAL:
						$whereSql	.= "<> '$valueWhere'";
					break;
					case $this->OPERATOR_LESS_THAN:
						$whereSql	.= "< $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN:
						$whereSql	.= "> $valueWhere";
					break;
					case $this->OPERATOR_LESS_THAN_EQUAL:
						$whereSql	.= "<= $valueWhere";
					break;
					case $this->OPERATOR_GREAT_THAN_EQUAL:
						$whereSql	.= ">= $valueWhere";
					break;
					case $this->OPERATOR_LIKE:
						$whereSql	.= "LIKE '%$valueWhere%'";
					break;
				}
			}
		}
		
		$sql		= "SELECT ".$tableName[1].".".$IdName.$fieldSql
					." FROM ".$tableName[0]." ".$tableName[1]." "
					.$innerJoinSql
					." WHERE (".$fieldWhereSql.")"
					.(($id!="")?" AND ".$tableName[1].".".$IdName."='".$id."'":"")
					.$whereSql." limit 10";
					$record		= $this->db->query($sql);
//		echo $sql;
		$resource	= $record->result_array();
		$this->printArr($resource, false);
		// Replace Field
		if(count($resource)>0)
		foreach($resource as $i=>$r)
		{
			foreach($fieldReplace as $fr)
			{
				if(is_array($fr['replace']))
				{
					$resource[$i][$fr['name']] = $this->fixedFalueFromArray($resource[$i][$fr['name']],$fr['replace']);
					
				}
			}
		}
		$this->printArr($resource, false);
		$this->printArr(array($resource, $textField), false);
		return array($resource, $textField, $id);
	}
	
	function getAdvanceTableList($arrayFieldSource, $count=10, $id="", $where = true)
	{
		$arrayFieldFilter	= $arrayFieldSource[4];
		$paramSearch		= $this->uri->segment(7);
		$paramSearch		= rawurldecode($paramSearch);
		$paramSearch		= ($paramSearch=="-")?"":$paramSearch;
		$arrayFilterValue 	= array();
		$sqlWhereFilter		= "";
		foreach($arrayFieldFilter as $k=>$v)
		{
			$arrayFilterValue[$k]	= $this->uri->segment(8+$k);
			$arrayFilterValue[$k]	= ($arrayFilterValue[$k]=="-")?"":$arrayFilterValue[$k];
			if($k>0)
			$sqlWhereFilter .= "and ";
			$sqlWhereFilter .= (($v[1][1]=="")?$v[1][0]:$v[1][1]).".".$v[2]." like '%".$arrayFilterValue[$k]."%' ";
		}
		$tableName	= $arrayFieldSource[2];
		$IdName		= $arrayFieldSource[1];

		$fieldsName	= $arrayFieldSource[0];
		$fieldSql	= "";
		$fieldWhereSql	= "";
		$textField		= array();
		$textField[0]	= "ID";
		$fieldReplace	= array();
		foreach($fieldsName as $i=>$field)
		{	
			$fieldSql .= ", ".$tableName[1].".".$field[0]." ".$field[1];
			if($field[2]==true)
			{
				if($i>0)
					$fieldWhereSql	.= "OR ";
				$fieldWhereSql		.= $tableName[1].".".$field[0]." like '%".$paramSearch."%' ";
			}
			$textField[count($textField)+1] = $field[3];
			$fieldReplace[$i]['name'] 		= ($field[1]=="")?$field[0]:$field[1];
			$fieldReplace[$i]['replace'] 	=  (isset($field[4]))?$field[4]:"";
		}
		
		$innerJoin		= $arrayFieldSource[3];
		$innerJoinSql	= "";
		foreach($innerJoin as $i=>$inner)
		{
			$aliasInnerTable = (isset($inner[0][1]))?$inner[0][1]:$inner[0][0];
			$innerJoinSql .= " INNER JOIN ".$inner[0][0]." ".$aliasInnerTable
							." ON ".$inner[1][1].".".$inner[3]." = ".$aliasInnerTable.".".$inner[2];
			foreach($inner[4] as $i=>$field)
			{	
				$fieldSql .= ", ".$aliasInnerTable.".".$field[0]." ".$field[1];
				if($field[2]==true)
				{
					if($fieldWhereSql!="")
						$fieldWhereSql	.="OR ";
					$fieldWhereSql	.= $aliasInnerTable.".".$field[0]." like '%".$paramSearch."%' ";
				}
				$textField[count($textField)+1] = $field[3];
				$fieldReplace[count($fieldReplace)]['name']		= ($field[1]=="")?$field[0]:$field[1];
				$fieldReplace[count($fieldReplace)-1]['replace'] 	=  (isset($field[4]))?$field[4]:"";
			}
		}

		$sql		= "SELECT ".$tableName[1].".".$IdName.$fieldSql
					." FROM ".$tableName[0]." ".$tableName[1]." "
					.$innerJoinSql;
		if($where)
			$sql	.= " WHERE (".$fieldWhereSql.") AND (".$sqlWhereFilter.")"
					.(($id!="")?" AND ".$tableName[1].".".$IdName."='".$id."'":"");
		else
			$sql	.= (($id!="")?" WHERE ".$tableName[1].".".$IdName."='".$id."'":"");
//		echo $sql."<br><br>";
		$record		= $this->db->query($sql);
		$resource	= $record->result_array();
		// Replace Field
		foreach($resource as $i=>$r)
		{
			foreach($fieldReplace as $fr)
			{
				if(is_array($fr['replace']))
				{
					$resource[$i][$fr['name']] = $this->fixedFalueFromArray($resource[$i][$fr['name']],$fr['replace']);
					
				}
			}
		}
		$this->printArr(array($resource, $textField, $id), false);
		return array($resource, $textField, $id);
	}

	function fixedFalueFromArray($value, $fixedValue = array(),$matchCase=true)
	{
		$retValue = $value;
		foreach($fixedValue as $fv)
		{
			if((($matchCase)?$fv[0]:strtolower($fv[0]))==(($matchCase)?$value:strtolower($value)))
			{
				$retValue = $fv[1]; 
				return $retValue;
			}
		}
		return $retValue;
	}
	
	function checkFalueFromArray($value, $fixedValue = array(),$matchCase=true)
	{
		foreach($fixedValue as $fv)
		{
			if((($matchCase)?$fv:strtolower($fv))==(($matchCase)?$value:strtolower($value)))
			{
				return true;
			}
		}
		return false;
	}
		
	function fixedFalueFromTable($value, $table = array())
	{
		$sql 	= "SELECT ".$table[1]." FROM ".$table[0]." WHERE ".$table[1]." = '".$value."'";
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
		if(count($res)>0)
			return true;
		return false;
	}
	
	function getTypeofFileName($fileName)
	{
		if(strlen($fileName)>5)
			return substr($fileName,strpos($fileName,".",strlen($fileName)-5)+1);
		return;
	}

	function getNameofFileName($fileName)
	{
		if(strlen($fileName)>5)
			return substr($fileName,0,strpos($fileName,".",strlen($fileName)-5));
		return;
	}
	
	function checkValidation($i_validationType, $value, $Text)
	{
		$returnValue['msg']		= $Text." => ";
		$returnValue['valid']	= true;
		switch($i_validationType[0])
		{
			case $this->VALIDATION_NONE:
			break;
			case $this->VALIDATION_NOTNULL:
				if($value=="")
				{
					$returnValue['msg']		.= "NOTNULL required.";
					$returnValue['valid']	= false;
				}
				else
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
			break;
			case $this->VALIDATION_NUMERIC:
				if(!is_numeric($value))
				{
					$returnValue['msg']		.= "Numeric required.";
					$returnValue['valid']	= false;
				}
				else
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
			break;
			case $this->VALIDATION_EMAIL:
				if((!filter_var($value, FILTER_VALIDATE_EMAIL)) &&($value!=""))
				{
					$returnValue['msg']		.= "Email required.";
					$returnValue['valid']	= false;
				}
				else
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
			break;
			case $this->VALIDATION_URL:
				if((!(substr($value, 0, 7)=='http://')) &&($value!=""))
				{
					$returnValue['msg']		.= "URL required. ex, http://google.com";
					$returnValue['valid']	= false;
				}
				else
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
			break;
			case $this->VALIDATION_DATE:
				$year	= substr($value,0,4);
				$month	= substr($value,5,2);
				$day	= substr($value,8,2);
				if(!checkdate($month,$day, $year))
				{
					$returnValue['msg']		.= "Date format required.";
					$returnValue['valid']	= false;
				}
				else
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
			break;
			case $this->VALIDATION_TIME:
				$hour	= substr($value, 0, 2);
				$minute	= substr($value, 3, 2);
				if(is_numeric($hour) && is_numeric($minute) && ($hour<=25) && ($minute<=60))
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
				else
				{
					$returnValue['msg']		.= "Time format required.";
					$returnValue['valid']	= false;
				}
			break;
			case $this->VALIDATION_MATCH_TABLE:
				if(!$this->fixedFalueFromTable($value, $i_validationType[1]))
				{
					$returnValue['msg']		.= "Match Table required.";
					$returnValue['valid']	= false;
				}
				else
				{
					$returnValue['msg']		.= "Verified.";
					$returnValue['valid']	= true;
				}
			break;
			case $this->VALIDATION_FILE_TYPE:
				if(is_array($value))
				{
					foreach($value as $v)
					{
						$v = strtolower($v);
						if((!$this->checkFalueFromArray($this->getTypeofFileName($v), $i_validationType[1])) && ($v!=''))
						{
							$returnValue['msg']		.= "Specifix File Type required [".implode("|", $i_validationType[1])."].";
							$returnValue['valid']	= false;
							break;
						}
					}
				}
				else
				{
					$value = strtolower($value);
					if((!$this->checkFalueFromArray($this->getTypeofFileName($value), $i_validationType[1])) && ($value!=''))
					{
						$returnValue['msg']		.= "Specifix File Type required [".implode("|", $i_validationType[1])."].";
						$returnValue['valid']	= false;
					}
					else
					{
						$returnValue['msg']		.= "Verified.";
						$returnValue['valid']	= true;
					}
				}
			break;
		}
		return $returnValue;
	}
	
	function printArr($arr, $print = true)
	{
		if($print)
		{
			echo "<pre>";
			print_r($arr);
			echo "</pre>";
		}
	}
	
	function updateData($arrField)
	{
		$this->load->library('upload');
		// upload image
		foreach($arrField as $idx=>$f)
		{
			$moduleName = $f[$this->ARRAY_INDEX_FIELD_NAME];
			switch ($f[$this->ARRAY_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_IMAGE:
					$oldFileName	= $this->getNameofFileName($f[$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]);
					$typeFileName	= $this->getTypeofFileName($f[$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]);
					$pathFile		= 
					$this->_arraySetting[$this->ARRAY_INDEX_SETTING_UPLOAD_PATH].
					$f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
					if(!is_dir($pathFile))
						mkdir($pathFile);
					$newFileName 	= Date("YmdHis")."-".$moduleName."-".$oldFileName.".".$typeFileName;
					$newFileName	= str_replace(" ","",$newFileName);
					$this->upload->set_upload_path($pathFile);
					$this->upload->setNewFileName($newFileName);
					if($f[$this->ARRAY_INDEX_FIELD_VALIDATION][0] == $this->VALIDATION_FILE_TYPE)
					{
						$allowedType = implode("|", $f[$this->ARRAY_INDEX_FIELD_VALIDATION][1]);
						$this->upload->set_allowed_types($allowedType);
					}
					if(!$this->_DEMO_MODE)
					{
						if($this->upload->do_upload($moduleName))
						{
							$file = $this->upload->data();
							if(is_array($f[$this->ARRAY_INDEX_FIELD_VALIDATION][2]))
								$this->resize_crop_image($file, $f[$this->ARRAY_INDEX_FIELD_VALIDATION][2]);
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1] = $pathFile."/".$newFileName;
							if(isset($f[$this->ARRAY_INDEX_FIELD_VALIDATION][3]) && ($f[$this->ARRAY_INDEX_FIELD_VALIDATION][3]))
							{
								if(file_exists($pathFile."/".$newFileName))
									unlink($pathFile."/".$newFileName);
							}
						}
						else
						{
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1] = "";
						}
					}
					else
					{
						$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1] = "";
					}
					break;
				case $this->INPUT_TYPE_FILE:
					$oldFileName	= $this->getNameofFileName($f[$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]);
					$typeFileName	= $this->getTypeofFileName($f[$this->ARRAY_INDEX_FIELD_REAL_VALUE][1]);
					$pathFile		= 
					$this->_arraySetting[$this->ARRAY_INDEX_SETTING_UPLOAD_PATH].
					$f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
					if(!is_dir($pathFile))
						mkdir($pathFile);
					$newFileName 	= Date("YmdHis")."-".$moduleName."-".$oldFileName.".".$typeFileName;
					$newFileName	= str_replace(" ","",$newFileName);
					$this->upload->set_upload_path($pathFile);
					$this->upload->setNewFileName($newFileName);
					if($f[$this->ARRAY_INDEX_FIELD_VALIDATION][0] == $this->VALIDATION_FILE_TYPE)
					{
						$allowedType = implode("|", $f[$this->ARRAY_INDEX_FIELD_VALIDATION][1]);
						$this->upload->set_allowed_types($allowedType);
					}
					if(!$this->_DEMO_MODE)
					{
						if($this->upload->do_upload($moduleName))
						{
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1] = $pathFile."/".$newFileName;
						}
						else
						{
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1] = "";
						}
					}
					else
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1] = "";
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
					$realValue = $f[$this->ARRAY_INDEX_FIELD_REAL_VALUE][1];
					$this->printArr($realValue, false);
					if(count($realValue)>0)
					for($q = 0; $q < count($realValue); $q++)
					{
						$oldFileName	= $this->getNameofFileName($realValue[$q][0]);
						$typeFileName	= $this->getTypeofFileName($realValue[$q][0]);
						$pathFile		= 
						$this->_arraySetting[$this->ARRAY_INDEX_SETTING_UPLOAD_PATH].
						$f[$this->ARRAY_INDEX_FIELD_SOURCE][4];
						if(!is_dir($pathFile))
							mkdir($pathFile);
						$newFileName 	= Date("YmdHis")."-".$realValue[$q][1]."-".$oldFileName.".".$typeFileName;
						$newFileName	= str_replace(" ","",$newFileName);
						$this->upload->set_upload_path($pathFile);
						$this->upload->setNewFileName($newFileName);
						if($f[$this->ARRAY_INDEX_FIELD_VALIDATION][0] == $this->VALIDATION_FILE_TYPE)
						{
							$allowedType = implode("|", $f[$this->ARRAY_INDEX_FIELD_VALIDATION][1]);
							$this->upload->set_allowed_types($allowedType);
						}
						if($this->upload->do_upload($realValue[$q][1]))
						{
							$file = $this->upload->data();
							if(is_array($f[$this->ARRAY_INDEX_FIELD_VALIDATION][2]))
								$this->resize_crop_image($file, $f[$this->ARRAY_INDEX_FIELD_VALIDATION][2]);
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][1][$q] = $pathFile."/".$newFileName;
						}
						else
						{
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_REAL_VALUE][$q][1] = "";
						}
				}
				break;
			}
			
		}
		// set Index
		$newFieldId = "";
		if($this->_arrayField[0][$this->ARRAY_INDEX_FIELD_VALUE]==$this->FIELD_VALUE_AUTO)
		{
			if($this->_arrayField[0][$this->ARRAY_INDEX_FIELD_REAL_VALUE]=="")
			{
				$fieldId	= $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_NAME];
				$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
				$sql 	= "SELECT `".$fieldId."` idField FROM ".$tableName." ORDER BY `".$fieldId."` DESC LIMIT 1";
				$rec	= $this->db->query($sql);
				$res	= $rec->result_array();
				if(count($res)>0)	
					$newFieldId = $res[0]['idField']+1;
				else
					$newFieldId = 1;
			}
			else
			{
				$newFieldId = $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_REAL_VALUE];
			}
		}
		elseif($this->_arrayField[0][$this->ARRAY_INDEX_FIELD_VALUE]==$this->FIELD_VALUE_DEFAULT)
		{
				$newFieldId = $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_REAL_VALUE];
		}
		
		$fieldId	= $this->_arrayField[0][$this->ARRAY_INDEX_FIELD_NAME];
		$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$sql 	= "SELECT `".$fieldId."` idField FROM ".$tableName
					." WHERE `".$fieldId."`='".$newFieldId."'";
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
// ini blum bener seharusnya ada update disini
		if(count($res)>0)	
		{
/*
			$this->db->query("DELETE FROM  $tableName"
					." WHERE `".$fieldId."`='".$newFieldId."'");
*/
			if($this->_arraySetting[$this->ARRAY_INDEX_SETTING_EDIT])
				$this->updateRecord($newFieldId);
		}
		else
		{
			if($this->_arraySetting[$this->ARRAY_INDEX_SETTING_INSERT])
				$this->insertRecord($newFieldId);
		}
		
		// insert tahap 2
	}
	
	function updateDataCross($arrField)
	{
		$this->load->library('upload');
		// upload image
		foreach($arrField as $idx=>$f)
		{
			$moduleName = 'fdt';
			if($f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE]==$this->uri->segment(6))
			{
				switch ($f[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
				{
					case $this->INPUT_TYPE_IMAGE:
						$oldFileName	= $this->getNameofFileName($f[$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][1]);
						$typeFileName	= $this->getTypeofFileName($f[$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][1]);
						$pathFile		= 
						$this->_arraySetting[$this->ARRAY_INDEX_SETTING_UPLOAD_PATH].
						$f[$this->ARRAY_INDEX_FIELD_CROSS_SOURCE][0];
						if(!is_dir($pathFile))
							mkdir($pathFile);
						$newFileName 	= Date("YmdHis")."-".$moduleName."-".$oldFileName.".".$typeFileName;
						$newFileName	= str_replace(" ","",$newFileName);
						$this->upload->set_upload_path($pathFile);
						$this->upload->setNewFileName($newFileName);
						if($f[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION][0] == $this->VALIDATION_FILE_TYPE)
						{
							$allowedType = implode("|", $f[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION][1]);
							$this->upload->set_allowed_types($allowedType);
						}
						if(!$this->_DEMO_MODE)
						if($this->upload->do_upload($moduleName))
						{
							$file = $this->upload->data();
							if(is_array($f[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION][2]))
								$this->resize_crop_image($file, $f[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION][2]);
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][1] = $pathFile."/".$newFileName;
						}
						else
						{
							$this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE][1] = "";
						}
					break;
				}
				$fieldIdVal 	= $f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE];
				$fieldDataVal 	= $this->_arrayField[$idx][$this->ARRAY_INDEX_FIELD_CROSS_REAL_VALUE];
				$this->printArr($fieldDataVal, false);
			}
			
		}
		$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$fieldId	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][0];
		$fieldData	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][1];
		$fieldExtra	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][2];

		$sql 	= "SELECT `".$fieldId."` idField FROM ".$tableName
					." WHERE `".$fieldId."`='".$fieldIdVal."'";
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
		if(count($res)>0)	
		{
			$this->updateRecordCross($fieldIdVal, $fieldDataVal);
		}
	}
	
	function insertRecord($newFieldId)
	{
		$tableName			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$sql				= "INSERT INTO `".$tableName."` ";
		$sqlFields			= "(";
		$sqlValues			= "(";
		$sqlMultipleValue	= array();
		$sqlMultipleImage	= array();
		$idMultipleValue	= 0;
		$idMultipleImage	= 0;
		
		$idtable			= "";
		$this->load->model("engine/mod_sys");
		foreach($this->_arrayField as $i=>$f)
		{
			$fieldName	= $f[$this->ARRAY_INDEX_FIELD_NAME];
			if($i==0)
			{
				$fieldValue	= $newFieldId;
				$idtable	= $fieldValue;
			}
			else
				$fieldValue	= $f[$this->ARRAY_INDEX_FIELD_REAL_VALUE];
			$rep = array("'");
			$fieldValue = str_replace($rep, "", $fieldValue);
			switch($f[$this->ARRAY_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_HIDDEN:
				case $this->INPUT_TYPE_TEXT:
				case $this->INPUT_TYPE_TEXT_PRIMARY:
				case $this->INPUT_TYPE_TEXT_AREA:
				case $this->INPUT_TYPE_TEXT_AREA_PLUS_EDITOR:
				case $this->INPUT_TYPE_RADIO:
				case $this->INPUT_TYPE_SELECT:
				case $this->INPUT_TYPE_SIMPLE_TABLE:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue."', ";
				break;
				case $this->INPUT_TYPE_PASSWORD:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$this->pass_word->encryptPass($fieldValue)."', ";
				break;
				case $this->INPUT_TYPE_MEDIUM_TABLE:
				case $this->INPUT_TYPE_ADVANCE_TABLE:
					$this->printArr($fieldValue, false);
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue[0]."', ";
				break;
				case $this->INPUT_TYPE_CHECKBOX:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".implode("|",$fieldValue)."', ";
				break;
				case $this->INPUT_TYPE_PASSWORD:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue."', ";
				break;
				case $this->INPUT_TYPE_IMAGE:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue[1]."', ";
				break;
				case $this->INPUT_TYPE_FILE:
					$sqlFields	.= "`".$fieldName."`, ";
					$sqlValues	.= "'".$fieldValue[1]."', ";
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
					$this->printArr($fieldValue, false);
					if(is_array($fieldValue))
					foreach($fieldValue as $v)
					{
						$multipleValueTableSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
						$multipleValueFieldSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$multipleValueIdSrc		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][2];

						$multipleValueTable		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
						$multipleValueIdPrimary	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][4];
						$multipleValueData		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][5];
						$multipleValueIdForeign	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][6];
						$sqlMultipleValue[$idMultipleValue] =
							"INSERT INTO `".$multipleValueTable."`"
							." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
							." VALUES"
							." ('".$newFieldId."', '".$v."')";
						$idMultipleValue++;
						$sqlmt = "SELECT `".$multipleValueIdSrc."` FROM `".$multipleValueTableSrc."`"
							." WHERE `".$multipleValueFieldSrc[0]."`='".$v."'";
						$rec = $this->db->query($sqlmt);
						$res = $rec->result_array();
						if(count($res)==0)
						{
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTableSrc."`"
								." (`".$multipleValueFieldSrc[0]."`)"
								." VALUES"
								." ('".$v."')";
							$idMultipleValue++;
						}
					}
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
					foreach($fieldValue as $v)
					{
						$multipleValueTableSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
						$multipleValueFieldSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$multipleValueIdSrc		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][2];

						$multipleValueTable		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
						$multipleValueIdPrimary	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][4];
						$multipleValueData		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][5];
						$multipleValueIdForeign	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][6];
						$sqlmt = "SELECT `".$multipleValueIdSrc."` FROM `".$multipleValueTableSrc."`"
							." WHERE `".$multipleValueFieldSrc[0]."`='".$v[1]."'";
						$rec = $this->db->query($sqlmt);
						$res = $rec->result_array();
						if((count($res)>0) && $v[0]!="")
						{
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTable."`"
								." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
								." VALUES"
								." ('".$newFieldId."', '".$v[0]."')";
							$idMultipleValue++;
						}
						elseif((count($res)>0) && $v[0]=="")
						{
							$sqlmt = "SELECT `".$multipleValueIdSrc."` idRec FROM `".$multipleValueTableSrc."`"
								." WHERE `".$multipleValueFieldSrc[0]."`='".$v[1]."'";
							$rec = $this->db->query($sqlmt);
							$res = $rec->result_array();
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTable."`"
								." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
								." VALUES"
								." ('".$newFieldId."', '".$res[0]['idRec']."')";
							$idMultipleValue++;
						}
						else
						{
							$sqlmt =
								"INSERT INTO `".$multipleValueTableSrc."`"
								." (`".$multipleValueFieldSrc[0]."`)"
								." VALUES"
								." ('".$v[1]."')";
							$this->db->query($sqlmt);
							$sqlmt = "SELECT `".$multipleValueIdSrc."` idRec FROM `".$multipleValueTableSrc."`"
								." WHERE `".$multipleValueFieldSrc[0]."`='".$v[1]."'";
							$rec = $this->db->query($sqlmt);
							$res = $rec->result_array();
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTable."`"
								." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
								." VALUES"
								." ('".$newFieldId."', '".$res[0]['idRec']."')";
							$idMultipleValue++;
						}
					}
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
					$this->printArr($fieldValue, false);
					if(is_array($fieldValue[1]))
					foreach($fieldValue[1] as $v)
					{
						
						$multipleValueTable		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
						$multipleValueIdPrimary	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$multipleValueIdForeign	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][2];
						$multipleValueImage		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
						$sqlMultipleImage[$idMultipleImage] =
							"INSERT INTO `".$multipleValueTable."`"
							." (`".$multipleValueIdForeign."`, `".$multipleValueImage."`)"
							." VALUES"
							." ('".$newFieldId."', '".$v."')";
						$idMultipleImage++;
					}
				break;
			}
		}
		$sqlFields = substr($sqlFields, 0, strlen($sqlFields)-2);
		$sqlValues = substr($sqlValues, 0, strlen($sqlValues)-2);
		$sqlFields .= ") ";
		$sqlValues .= ") ";
		$sql		= $sql.$sqlFields." VALUES ".$sqlValues;
		if(!$this->_DEMO_MODE)
				$this->db->query($sql);
		foreach($sqlMultipleValue as $mV)
		{
			if(!$this->_DEMO_MODE)
						$this->db->query($mV);
		}
		foreach($sqlMultipleImage as $iV)
		{
			if(!$this->_DEMO_MODE)
						$this->db->query($iV);
		}
		$this->mod_sys->insertLog(
				$this->_SETTING_USER_OTORITY					// kategori
				, $this->_IDUSER		// kode user
				, "insert"				// type transaksi
				, $idtable				// id table
				, $tableName);			// nama tabel
		// Input Log
	}

	function updateRecord($newFieldId)
	{
		$tableName			= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$sql				= "UPDATE `".$tableName."` SET ";
		$sqlFields			= "";
		$sqlMultipleValue	= array();
		$idMultipleValue	= 0;
		$fieldIdName		= "";
		$this->load->model("engine/mod_sys");
		foreach($this->_arrayField as $i=>$f)
		{
			$fieldName	= $f[$this->ARRAY_INDEX_FIELD_NAME];
			if($i==0)
			{
				$fieldValue		= $newFieldId;
				$fieldIdName	= $fieldName;
			}
			else
				$fieldValue	= $f[$this->ARRAY_INDEX_FIELD_REAL_VALUE];
			switch($f[$this->ARRAY_INDEX_FIELD_INPUT][0])
			{
				case $this->INPUT_TYPE_HIDDEN:
				case $this->INPUT_TYPE_TEXT:
				case $this->INPUT_TYPE_TEXT_PRIMARY:
				case $this->INPUT_TYPE_TEXT_AREA:
				case $this->INPUT_TYPE_TEXT_AREA_PLUS_EDITOR:
				case $this->INPUT_TYPE_RADIO:
				case $this->INPUT_TYPE_SELECT:
				case $this->INPUT_TYPE_SIMPLE_TABLE:
					$sqlFields	.= "`".$fieldName."` = '".$fieldValue."', ";
				break;
				case $this->INPUT_TYPE_PASSWORD:
					if($fieldValue!="")
						$sqlFields	.= "`".$fieldName."` = '".$this->pass_word->encryptPass($fieldValue)."', ";
				break;
				case $this->INPUT_TYPE_MEDIUM_TABLE:
				case $this->INPUT_TYPE_ADVANCE_TABLE:
					$sqlFields	.= "`".$fieldName."` = '".$fieldValue[0]."', ";
				break;
				case $this->INPUT_TYPE_CHECKBOX:
					$sqlFields	.= "`".$fieldName."` = '".implode("|",$fieldValue)."', ";
				break;
				case $this->INPUT_TYPE_PASSWORD:
					$sqlFields	.= "`".$fieldName."` = '".$fieldValue."', ";
				break;
				case $this->INPUT_TYPE_IMAGE:
					if(($fieldValue[1]!='') || ($fieldValue[2]))
					{
						$setting	= $f[$this->ARRAY_INDEX_FIELD_VALIDATION][2];
						$this->deleteImage($tableName, $fieldName, $fieldIdName, $newFieldId, $setting);
						$sqlFields	.= "`".$fieldName."` = '".$fieldValue[1]."', ";
					}
				break;
				case $this->INPUT_TYPE_FILE:
					if(($fieldValue[1]!='') || ($fieldValue[2]))
					{
						$this->deleteFile($tableName, $fieldName, $fieldIdName, $newFieldId);
						$sqlFields	.= "`".$fieldName."` = '".$fieldValue[1]."', ";
					}
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE:
					$multipleValueTableSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
					$multipleValueFieldSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
					$multipleValueIdSrc		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][2];

					$multipleValueTable		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
					$multipleValueIdPrimary	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][4];
					$multipleValueData		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][5];
					$multipleValueIdForeign	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][6];
					$sqlMultipleValue[$idMultipleValue] = 
						"DELETE FROM `".$multipleValueTable."` "
						."WHERE `$multipleValueIdForeign` = '$newFieldId'";
					$idMultipleValue++;
					foreach($fieldValue as $v)
					{

						$sqlMultipleValue[$idMultipleValue] =
							"INSERT INTO `".$multipleValueTable."`"
							." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
							." VALUES"
							." ('".$newFieldId."', '".$v."')";
						$idMultipleValue++;
						$sqlmt = "SELECT `".$multipleValueIdSrc."` FROM `".$multipleValueTableSrc."`"
							." WHERE `".$multipleValueFieldSrc[0]."`='".$v."'";
						$rec = $this->db->query($sqlmt);
						$res = $rec->result_array();
						if(count($res)==0)
						{
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTableSrc."`"
								." (`".$multipleValueFieldSrc[0]."`)"
								." VALUES"
								." ('".$v."')";
							$idMultipleValue++;
						}
					}
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_OUT_TABLE_FOREIGN:
					$multipleValueTableSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
					$multipleValueFieldSrc	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
					$multipleValueIdSrc		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][2];

					$multipleValueTable		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
					$multipleValueIdPrimary	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][4];
					$multipleValueData		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][5];
					$multipleValueIdForeign	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][6];
					$sqlMultipleValue[$idMultipleValue] = 
							"DELETE FROM `".$multipleValueTable."` "
							."WHERE `$multipleValueIdForeign` = '$newFieldId'";
					$idMultipleValue++;
					foreach($fieldValue as $v)
					{
						$sqlmt = "SELECT `".$multipleValueIdSrc."` FROM `".$multipleValueTableSrc."`"
							." WHERE `".$multipleValueFieldSrc[0]."`='".$v[1]."'";
						$rec = $this->db->query($sqlmt);
						$res = $rec->result_array();
						if((count($res)>0) && $v[0]!="")
						{
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTable."`"
								." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
								." VALUES"
								." ('".$newFieldId."', '".$v[0]."')";
							$idMultipleValue++;
						}
						elseif((count($res)>0) && $v[0]=="")
						{
							$sqlmt = "SELECT `".$multipleValueIdSrc."` idRec FROM `".$multipleValueTableSrc."`"
								." WHERE `".$multipleValueFieldSrc[0]."`='".$v[1]."'";
							$rec = $this->db->query($sqlmt);
							$res = $rec->result_array();
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTable."`"
								." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
								." VALUES"
								." ('".$newFieldId."', '".$res[0]['idRec']."')";
							$idMultipleValue++;
						}
						else
						{
							$sqlmt =
								"INSERT INTO `".$multipleValueTableSrc."`"
								." (`".$multipleValueFieldSrc[0]."`)"
								." VALUES"
								." ('".$v[1]."')";
							$this->db->query($sqlmt);
							$sqlmt = "SELECT `".$multipleValueIdSrc."` idRec FROM `".$multipleValueTableSrc."`"
								." WHERE `".$multipleValueFieldSrc[0]."`='".$v[1]."'";
							$rec = $this->db->query($sqlmt);
							$res = $rec->result_array();
							$sqlMultipleValue[$idMultipleValue] =
								"INSERT INTO `".$multipleValueTable."`"
								." (`".$multipleValueIdForeign."`, `".$multipleValueData."`)"
								." VALUES"
								." ('".$newFieldId."', '".$res[0]['idRec']."')";
							$idMultipleValue++;
						}
					}
				break;
				case $this->INPUT_TYPE_MULTIPLE_VALUE_IMAGE:
					$this->printArr($fieldValue, false);
					if(is_array($fieldValue[2]))
					{
						$setting		= $f[$this->ARRAY_INDEX_FIELD_VALIDATION][2];
						$miTableName	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
						$miForeignId	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$miId			= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$miData			= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
							
						foreach($fieldValue[2] as $fid)
						{
							$this->deleteImage($miTableName, $miData, $miId, $fid, $setting);
							$sqlMultipleValue[$idMultipleValue] = 
								"DELETE FROM `".$miTableName."` "
								."WHERE `$miId` = '$fid'";
							$idMultipleValue++;
						}
					}
					if(is_array($fieldValue[1]))
					foreach($fieldValue[1] as $v)
					{
						$multipleValueTable		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][0];
						$multipleValueIdPrimary	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][1];
						$multipleValueIdForeign	= $f[$this->ARRAY_INDEX_FIELD_SOURCE][2];
						$multipleValueImage		= $f[$this->ARRAY_INDEX_FIELD_SOURCE][3];
						$sqlMultipleValue[$idMultipleValue] =
							"INSERT INTO `".$multipleValueTable."`"
							." (`".$multipleValueIdForeign."`, `".$multipleValueImage."`)"
							." VALUES"
							." ('".$newFieldId."', '".$v."')";
						$idMultipleValue++;
					}
				break;
			}
		}
		$sqlFields = substr($sqlFields, 0, strlen($sqlFields)-2);
		$sql		= $sql.$sqlFields
		." WHERE `$fieldIdName` = '$newFieldId'";
//		echo $sql;
		if(!$this->_DEMO_MODE)
			$this->db->query($sql);
		foreach($sqlMultipleValue as $mV)
		{
			$this->db->query($mV);
		}
		$this->mod_sys->insertLog(
				$this->_SETTING_USER_OTORITY					// kategori
				, $this->_IDUSER		// kode user
				, "update"				// type transaksi
				, $newFieldId			// id table
				, $tableName);			// nama tabel
		// Input Log
	}
	
	function updateRecordCross($fieldIdVal, $fieldDataVal)
	{
		$tableName	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_TABLE_NAME];
		$fieldId	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][0];
		$fieldData	= $this->_arraySetting[$this->ARRAY_INDEX_SETTING_CROSS_FIELD][1];
		$sql				= "UPDATE `".$tableName."` SET ";
		$sqlFields			= "";
		foreach($this->_arrayField as $i=>$f)
		{
//			print_r($fieldDataVal);
//			echo "<br>".$f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE]." - ".$fieldIdVal."<br>";
			if($f[$this->ARRAY_INDEX_FIELD_CROSS_VALUE]==$fieldIdVal)
			switch($f[$this->ARRAY_INDEX_FIELD_CROSS_INPUT][0])
			{
				case $this->INPUT_TYPE_TEXT:
				case $this->INPUT_TYPE_TEXT_AREA:
				case $this->INPUT_TYPE_SELECT:
				case $this->INPUT_TYPE_SIMPLE_TABLE:
				case $this->INPUT_TYPE_RADIO:
					$sqlFields	.= "`".$fieldData."` = '".$fieldDataVal."'";
				break;
				case $this->INPUT_TYPE_IMAGE:
					if(($fieldDataVal[1]!=''))
					{
						$setting	= $f[$this->ARRAY_INDEX_FIELD_CROSS_VALIDATION][2];
						$this->deleteImage($tableName, $fieldData, $fieldId, $fieldIdVal, $setting);
						$sqlFields	.= "`".$fieldData."` = '".$fieldDataVal[1]."' ";
					}
				break;
			}
		}
		$sql		= $sql.$sqlFields
		." WHERE `$fieldId` = '$fieldIdVal'";
//		echo $sql;
		if($sqlFields!="")
			if(!$this->_DEMO_MODE)
				$this->db->query($sql);
	}
	
	function deleteImage($tableName, $data, $id, $idValue, $sizeSetting)
	{
		$sql	= "SELECT `$data` FROM `$tableName` WHERE `$id` = '$idValue'";
//		echo $sql;
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
		if(!$this->_DEMO_MODE)
		if($rec->num_rows()>0)
		{
			$fileName	= $res[0][$data];
			if(file_exists($fileName))
			{
				unlink($fileName);
			}
			$nameFile	= $this->getNameofFileName($fileName);
			$typeFile	= $this->getTypeofFileName($fileName);
			foreach($sizeSetting as $set)
			{
				$childName	= $nameFile.'-'.$set['initial'].".".$typeFile;
//				echo $childName."<br>"; 
				if(file_exists($childName))
					unlink($childName);
			}
		}
	}

	function deleteFile($tableName, $data, $id, $idValue)
	{
		$sql	= "SELECT `$data` FROM `$tableName` WHERE `$id` = '$idValue'";
//		echo $sql;
		$rec	= $this->db->query($sql);
		$res	= $rec->result_array();
		if($rec->num_rows()>0)
		{
			$fileName	= $res[0][$data];
			if(file_exists($fileName))
			{
				unlink($fileName);
			}
		}
	}
	
	function resize_crop_image($fileData, $resizeData)
	{
		$this->load->library('image_lib');
		foreach($resizeData as $res)
		{
			$newResFilename = $fileData['raw_name']."-"
								.$res['initial']
								.$fileData['file_ext'];
			copy($fileData['full_path'], $fileData['file_path'].$newResFilename);
			$newWidth					= $res['width'];
			$newHeight					= $res['height'];
			$config['image_library']	= "gd2";
			$config['source_image']		= $fileData['file_path'].$newResFilename;
			if($res['type']=='resize')
			{
				if($newWidth>0 & $newHeight == 0)
				{
					$newHeight = floor(($fileData['image_height']/$fileData['image_width'])*$newWidth);
				}
				elseif($newWidth==0 & $newHeight > 0)
				{
					$newWidth = floor(($fileData['image_width']/$fileData['image_height'])*$newHeight);
				}
				$config['maintain_ratio'] = TRUE;
				$config['width'] 	= $newWidth;
				$config['height'] 	= $newHeight;
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
			}
			elseif($res['type']=='crop')
			{
				$orgW = $newWidth;
				$orgH = $newHeight;
				if(($newHeight > 0) && ($newWidth>0))
				{
					if($fileData['image_width']>$fileData['image_height'])
					{
						$newHeight 	= floor(($fileData['image_height']/$fileData['image_width'])*$newWidth);
					}
					else
					{
						$newWidth 	= ($fileData['image_height']>$newHeight)?$newHeight:$fileData['image_height'];
						$newHeight 	= floor(($fileData['image_height']/$fileData['image_width'])*$newWidth);
					}
					
				}
				elseif($fileData['image_width']>$fileData['image_height'])
				{
					$newHeight 	= $newWidth;
					$newWidth 	= floor(($fileData['image_width']/$fileData['image_height'])*$newHeight);
				}
				else
				{
					$newHeight = floor(($fileData['image_height']/$fileData['image_width'])*$newWidth);
				}
				$config['maintain_ratio'] = TRUE;
				$config['width'] 	= $newWidth;
				$config['height'] 	= $newHeight;
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
				
				$x_axis = 0;
				$y_axis	= 0;

				if($orgW>0 && $orgH>0)
				{
					$y_axis 	= ($newHeight-$orgH)/2;
					$newHeight	= $orgH;
				}
				elseif($newWidth>$newHeight)
				{
					$x_axis 	= ($newWidth-$newHeight)/2;
					$newWidth	= $newHeight;
				}
				else
				{
					$y_axis 	= ($newHeight-$newWidth)/2;
					$newHeight=$newWidth;
				}
				$config['maintain_ratio'] = false;
				$config['width'] 	= $newWidth;
				$config['height'] 	= $newHeight;
				$config['x_axis'] 	= $x_axis;
				$config['y_axis'] 	= $y_axis;
				$this->image_lib->initialize($config);
				$this->image_lib->crop();
			}
			$this->image_lib->clear();
		}
	}
	
	function getMenu($otority, $idUser)
	{
		$returnValue	= "";
		$sql			= "SELECT id_menu
							, title
							, url
							, image_name
							, `system`
							FROM tbl_menu_cms
							WHERE user_otority='".$otority."'
							AND parent = 0";
		$rec			= $this->db->query($sql);
		$res			= $rec->result_array();
		foreach($res as $i=>$v)
		{
			$returnValue[$i][0]	= $v;
			if($otority=='admin')
				$sql2			= "SELECT tmc.id_menu
								, tmc.title
								, tmc.url
								, tmc.image_name
								, tmc.system
								FROM tbl_menu_cms tmc
								INNER JOIN tbl_admin_category_akses taca
									ON taca.id_menu = tmc.id_menu
								INNER JOIN tbl_admin_user tau
									ON taca.id_admin_category = tau.id_category
								WHERE tmc.user_otority='".$otority."'
								AND tmc.parent = '".$v['id_menu']."'"
								." AND tau.id_admin_user = '$idUser'
								ORDER BY tmc.id_menu ASC";
			else
				$sql2			= "SELECT tmc.id_menu
								, tmc.title
								, tmc.url
								, tmc.image_name
								, tmc.system
								FROM tbl_menu_cms tmc
								WHERE tmc.user_otority='".$otority."'
								AND tmc.parent = '".$v['id_menu']."'
								ORDER BY tmc.id_menu ASC";
			
			$rec2			= $this->db->query($sql2);
			$res2			= $rec2->result_array();
			$returnValue[$i][1]	= $res2;
		}
		$this->printArr($returnValue, false);
		return $returnValue;
	}
}
?>